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?
This question is related to
python
text
matplotlib
axis-labels
keyword-argument
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.
It is simpler than I thought it would be.
To set the font size of the x-axis ticks:
x_ticks=['x tick 1','x tick 2','x tick 3']
ax.set_xticklabels(x_ticks, rotation=0, fontsize=8)
To do it for the y-axis ticks:
y_ticks=['y tick 1','y tick 2','y tick 3']
ax.set_yticklabels(y_ticks, rotation=0, fontsize=8)
The arguments rotation
and fontsize
can easily control what I was after.
Reference: http://matplotlib.org/api/axes_api.html
Use the keyword size
instead of fontsize
.
Source: Stackoverflow.com