Python: How to increase/reduce the fontsize of x and y tick labels?

FaCoffee picture FaCoffee · Nov 30, 2015 · Viewed 104.8k times · Source

I seem to have a problem in figuring out how to increase or decrease the fontsize of both the x and y tick labels while using matplotlib.

I am aware that there is the set_xticklabels(labels, fontdict=None, minor=False, **kwargs) function, but I failed to understand how to control the fontsize in it.

I expected something somehow explicit, like

title_string=('My Title')
plt.suptitle(title_string, y=1.0, fontsize=17)

but I haven't found anything like that so far. What am I missing?

Answer

ImportanceOfBeingErnest picture ImportanceOfBeingErnest · Apr 3, 2019

One shouldn't use set_yticklabels to change the fontsize, since this will also set the labels (i.e. it will replace any automatic formatter by a FixedFormatter), which is usually undesired. The easiest is to set the respective tick_params:

ax.tick_params(axis="x", labelsize=8)
ax.tick_params(axis="y", labelsize=20)

or

ax.tick_params(labelsize=8)

in case both axes shall have the same size.

Of course using the rcParams as in @tmdavison's answer is possible as well.