Python Forum

Full Version: How to auto align x-axis label
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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?
(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!!