flow flow - 1 year ago 82
Python Question

Matplotlib make tick labels font size smaller

In a matplotlib figure, how can I make the font size for the tick labels using


Further, how can one rotate it from horizontal to vertical?

Answer Source

Please note that newer versions of MPL have a shortcut for this task. An example is shown in the other answer to this question: http://stackoverflow.com/a/11386056/42346

The code below is for illustrative purposes and may not necessarily be optimized.

import matplotlib.pyplot as plt
import numpy as np

def xticklabels_example():
    fig = plt.figure() 

    x = np.arange(20)
    y1 = np.cos(x)
    y2 = (x**2)
    y3 = (x**3)
    yn = (y1,y2,y3)
    COLORS = ('b','g','k')

    for i,y in enumerate(yn):
        ax = fig.add_subplot(len(yn),1,i+1)

        ax.plot(x, y, ls='solid', color=COLORS[i]) 

        if i != len(yn) - 1:
            # all but last 
            ax.set_xticklabels( () )
            for tick in ax.xaxis.get_major_ticks():
                # specify integer or one of preset strings, e.g.

    fig.suptitle('Matplotlib xticklabels Example')

if __name__ == '__main__':

enter image description here