tick labels on x-axis in matplotlib histogram

Question:

I have created histogram successfully but the tick labels are very wide. I am want smaller tick labels like ‘1.5-1.75’ ‘1.75-2.0’. This is my code:

    binlist = [-10.0, -9.95, -9.9, -9.85, -9.8, -9.75, -9.7, -9.65, -9.6, -9.55, -9.5, -9.45, -9.4, -9.35, -9.3, -9.25, -9.2, -9.15, -9.1, -9.05, -9.0, -8.95, -8.9, -8.85, -8.8, -8.75, -8.7, -8.65, -8.6, -8.55, -8.5, -8.45, -8.4, -8.35, -8.3, -8.25, -8.2, -8.15, -8.1, -8.05, -7.99, -7.94, -7.89, -7.84, -7.79, -7.74, -7.69, -7.64, -7.59, -7.54, -7.49, -7.44, -7.39, -7.34, -7.29, -7.24, -7.19, -7.14, -7.09, -7.04, -6.99, -6.94, -6.89, -6.84, -6.79, -6.74, -6.69, -6.64, -6.59, -6.54, -6.49, -6.44, -6.39, -6.34, -6.29, -6.24, -6.19, -6.14, -6.09, -6.04, -5.99, -5.94, -5.89, -5.84, -5.79, -5.74, -5.69, -5.64, -5.59, -5.54, -5.49, -5.44, -5.39, -5.34, -5.29, -5.24, -5.19, -5.14, -5.09, -5.04, -4.99, -4.94, -4.89, -4.84, -4.79, -4.74, -4.69, -4.64, -4.59, -4.54, -4.49, -4.44, -4.39, -4.34, -4.29, -4.24, -4.19, -4.14, -4.09, -4.04, -3.98, -3.93, -3.88, -3.83, -3.78, -3.73, -3.68, -3.63, -3.58, -3.53, -3.48, -3.43, -3.38, -3.33, -3.28, -3.23, -3.18, -3.13, -3.08, -3.03, -2.98, -2.93, -2.88, -2.83, -2.78, -2.73, -2.68, -2.63, -2.58, -2.53, -2.48, -2.43, -2.38, -2.33, -2.28, -2.23, -2.18, -2.13, -2.08, -2.03, -1.98, -1.93, -1.88, -1.83, -1.78, -1.73, -1.68, -1.63, -1.58, -1.53, -1.48, -1.43, -1.38, -1.33, -1.28, -1.23, -1.18, -1.13, -1.08, -1.03, -0.98, -0.93, -0.88, -0.83, -0.78, -0.73, -0.68, -0.63, -0.58, -0.53, -0.48, -0.43, -0.38, -0.33, -0.28, -0.23, -0.18, -0.13, -0.08, -0.03, 0.03, 0.08, 0.13, 0.18, 0.23, 0.28, 0.33, 0.38, 0.43, 0.48, 0.53, 0.58, 0.63, 0.68, 0.73, 0.78, 0.83, 0.88, 0.93, 0.98, 1.03, 1.08, 1.13, 1.18, 1.23, 1.28, 1.33, 1.38, 1.43, 1.48, 1.53, 1.58, 1.63, 1.68, 1.73, 1.78, 1.83, 1.88, 1.93, 1.98, 2.03, 2.08, 2.13, 2.18, 2.23, 2.28, 2.33, 2.38, 2.43, 2.48, 2.53, 2.58, 2.63, 2.68, 2.73, 2.78, 2.83, 2.88, 2.93, 2.98, 3.03, 3.08, 3.13, 3.18, 3.23, 3.28, 3.33, 3.38, 3.43, 3.48, 3.53, 3.58, 3.63, 3.68, 3.73, 3.78, 3.83, 3.88, 3.93, 3.98, 4.04, 4.09, 4.14, 4.19, 4.24, 4.29, 4.34, 4.39, 4.44, 4.49, 4.54, 4.59, 4.64, 4.69, 4.74, 4.79, 4.84, 4.89, 4.94, 4.99, 5.04, 5.09, 5.14, 5.19, 5.24, 5.29, 5.34, 5.39, 5.44, 5.49, 5.54, 5.59, 5.64, 5.69, 5.74, 5.79, 5.84, 5.89, 5.94, 5.99, 6.04, 6.09, 6.14, 6.19, 6.24, 6.29, 6.34, 6.39, 6.44, 6.49, 6.54, 6.59, 6.64, 6.69, 6.74, 6.79, 6.84, 6.89, 6.94, 6.99, 7.04, 7.09, 7.14, 7.19, 7.24, 7.29, 7.34, 7.39, 7.44, 7.49, 7.54, 7.59, 7.64, 7.69, 7.74, 7.79, 7.84, 7.89, 7.94, 7.99, 8.05, 8.1, 8.15, 8.2, 8.25, 8.3, 8.35, 8.4, 8.45, 8.5, 8.55, 8.6, 8.65, 8.7, 8.75, 8.8, 8.85, 8.9, 8.95, 9.0, 9.05, 9.1, 9.15, 9.2, 9.25, 9.3, 9.35, 9.4, 9.45, 9.5, 9.55, 9.6, 9.65, 9.7, 9.75, 9.8, 9.85, 9.9, 9.95, 10.0]

with PdfPages('multipage_pdf.pdf') as pdf:
    for k, v in mega_data4.items():
        se = pd.Series(v)
        df = pd.DataFrame()
        df[k] = se.values
        df.hist(bins=binlist)
        pdf.savefig()
        plt.close()

How to get small ticks labels on the x-axis?

enter image description here

Asked By: KawaiKx

||

Answers:

You can set custom xticks using plt.xticks.

e.g.

# Change this to whatever values you want
tick_values = list(range(-10, 11, 2)) # Create a tick every 2 steps

with PdfPages('multipage_pdf.pdf') as pdf:
    for k, v in mega_data4.items():
        se = pd.Series(v)
        df = pd.DataFrame()
        df[k] = se.values
        df.hist(bins=binlist)
        plt.xticks(tick_values)
        pdf.savefig()
        plt.close()
Answered By: Angus Nicolson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.