Python Forum
How to auto align x-axis label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to auto align x-axis label
#1
Hi all,

A matplotlib plt.bar demo create a chart as snapshots, how to set up x-axis label no shadow,thanks!!

import matplotlib.pyplot as plt


plt.style.use('ggplot')

labels = ['MEMS Vbias', 'AVDD Continuity', 'DVDD Continuity', 'IIL CLK', 'Ref Sens ADC Gain Final', 'YL_gest_NM','Y1_HSPL_LPM_offset_trim', 'Current_fail', 'Out of Range']
values = [10, 15, 12, 9, 7, 13, 1, 1, 1]


fig = plt.figure(figsize=(10, 5))

# creating the bar plot
plt.bar(labels, values, color=['black','red','green','blue','cyan',])

plt.ylabel("Failed_QTY")
plt.title("Tester Failed overview")


plt.show()
Reply
#2
What's the problem? You made the chart too small for the long labels. You can:
1 Make the chart bigger
2 User shorter labels
3 Specify a smaller font for the labels
4 Rotate the labels

You could do this to the labels:
labels = [
    "MEMS\nVbias",
    "AVDD\nContinuity",
    "DVDD\nContinuity",
    "IIL\nCLK",
    "Ref Sens\nADC Gain\nFinal",
    "YL_gest_NM",
    "Y1_HSPL\nLPM_noffset\ntrim",
    "Current\nfail",
    "Out of\nRange",
Or rotate the labels.
import matplotlib.pyplot as plt


plt.style.use("ggplot")

labels = [
    "MEMS Vbias",
    "AVDD Continuity",
    "DVDD Continuity",
    "IIL CLK",
    "Ref Sens ADC Gain Final",
    "YL_gest_NM",
    "Y1_HSPL_LPM_offset_trim",
    "Current_fail",
    "Out of Range",
]
values = [10, 15, 12, 9, 7, 13, 1, 1, 1]


fig = plt.figure(figsize=(10, 5))

# creating the bar plot
plt.bar(
    labels,
    values,
    color=[
        "black",
        "red",
        "green",
        "blue",
        "cyan",
    ],
)

plt.ylabel("Failed_QTY")
plt.title("Tester Failed overview")
plt.xticks(rotation=45)
plt.tight_layout()

plt.show()
My preference is shorter labels. Do you really need "Ref Sens ADC Gain Final" instead of "Reference\nGain". Can you replace "Y1_HSPL_LPM_offset_trim" with shorter gobbledygook?
Reply
#3
(Jan-27-2023, 04:29 PM)deanhystad Wrote: What's the problem? You made the chart too small for the long labels. You can:
1 Make the chart bigger
2 User shorter labels
3 Specify a smaller font for the labels
4 Rotate the labels

You could do this to the labels:
labels = [
    "MEMS\nVbias",
    "AVDD\nContinuity",
    "DVDD\nContinuity",
    "IIL\nCLK",
    "Ref Sens\nADC Gain\nFinal",
    "YL_gest_NM",
    "Y1_HSPL\nLPM_noffset\ntrim",
    "Current\nfail",
    "Out of\nRange",
Or rotate the labels.
import matplotlib.pyplot as plt


plt.style.use("ggplot")

labels = [
    "MEMS Vbias",
    "AVDD Continuity",
    "DVDD Continuity",
    "IIL CLK",
    "Ref Sens ADC Gain Final",
    "YL_gest_NM",
    "Y1_HSPL_LPM_offset_trim",
    "Current_fail",
    "Out of Range",
]
values = [10, 15, 12, 9, 7, 13, 1, 1, 1]


fig = plt.figure(figsize=(10, 5))

# creating the bar plot
plt.bar(
    labels,
    values,
    color=[
        "black",
        "red",
        "green",
        "blue",
        "cyan",
    ],
)

plt.ylabel("Failed_QTY")
plt.title("Tester Failed overview")
plt.xticks(rotation=45)
plt.tight_layout()

plt.show()
My preference is shorter labels. Do you really need "Ref Sens ADC Gain Final" instead of "Reference\nGain". Can you replace "Y1_HSPL_LPM_offset_trim" with shorter gobbledygook?



I appreciated you that is what I want, thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQT5 - align left frohr 7 3,835 May-07-2022, 09:56 PM
Last Post: deanhystad
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,232 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,426 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Center align Kristenl2784 1 1,918 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  How to left align logging messages Mekala 3 6,699 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  How to left align the columns SriRajesh 6 3,907 Dec-28-2019, 04:04 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020