python – Matplotlib make tick labels font size smaller

The Question :

334 people think this question is useful

In a matplotlib figure, how can I make the font size for the tick labels using ax1.set_xticklabels() smaller?

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

The Question Comments :
  • Thanks for asking this question, I’m trying to solve this problem right now. I’m not strongly opinionated here, but looks like voters think the best answer is not the accepted one. What do you think?
  • did I properly read that there are at least 5 different ways to change the fontsize in matplotlib? 😀

The Answer 1

141 people think this answer is useful

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

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

The Answer 2

570 people think this answer is useful

There is a simpler way actually. I just found:

import matplotlib.pyplot as plt
# We prepare the plot  
fig, ax = plt.subplots()

# We change the fontsize of minor ticks label 
ax.tick_params(axis='both', which='major', labelsize=10)
ax.tick_params(axis='both', which='minor', labelsize=8)

This only answers to the size of label part of your question though.

The Answer 3

249 people think this answer is useful

To specify both font size and rotation at the same time, try this:

plt.xticks(fontsize=14, rotation=90)

The Answer 4

56 people think this answer is useful

Alternatively, you can just do:

import matplotlib as mpl
label_size = 8
mpl.rcParams['xtick.labelsize'] = label_size 

The Answer 5

40 people think this answer is useful
plt.tick_params(axis='both', which='minor', labelsize=12)

The Answer 6

37 people think this answer is useful

Another alternative

I have two plots side by side and would like to adjust tick labels separately.

The above solutions were close however they were not working out for me. I found my solution from this matplotlib page.


This did the trick and was straight to the point. For my use case, it was the plot on the right that needed to be adjusted. For the plot on the left since I was creating new tick labels I was able to adjust the font in the same process as seting the labels.


ax1.set_xticklabels(ax1_x, fontsize=15)
ax1.set_yticklabels(ax1_y, fontsize=15)

thus I used for the right plot,


A minor subtlety… I know… but I hope this helps someone 🙂

Bonus points if anyone knows how to adjust the font size of the order of magnitude label.

enter image description here

The Answer 7

22 people think this answer is useful

In current versions of Matplotlib, you can do axis.set_xticklabels(labels, fontsize='small').

The Answer 8

15 people think this answer is useful

You can also change label display parameters like fontsize with a line like this:

zed = [tick.label.set_fontsize(14) for tick in ax.yaxis.get_major_ticks()]

The Answer 9

14 people think this answer is useful

For smaller font, I use

ax1.set_xticklabels(xticklabels, fontsize=7)

and it works!

The Answer 10

11 people think this answer is useful

The following worked for me:


The advantage of the above is you do not need to provide the array of labels and works with any data on the axes.

Add a Comment