Python: How to increase/reduce the fontsize of x and y tick labels?
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.
You can set the fontsize directly in the call to set_xticklabels
and set_yticklabels
(as noted in previous answers). This will only affect one Axes
at a time.
ax.set_xticklabels(x_ticks, rotation=0, fontsize=8)ax.set_yticklabels(y_ticks, rotation=0, fontsize=8)
Note this method should only be used if you are fixing the positions of the ticks first (e.g. using ax.set_xticks
). If you are not changing the tick positions from the default ones, you can just change the font size of the tick labels without changing the text using ax.tick_params
ax.tick_params(axis='x', labelsize=8)ax.tick_params(axis='y', labelsize=8)
or
ax.tick_params(axis='both', labelsize=8)
You can also set the ticklabel
font size globally (i.e. for all figures/subplots in a script) using rcParams
:
import matplotlib.pyplot as pltplt.rc('xtick',labelsize=8)plt.rc('ytick',labelsize=8)
Or, equivalently:
plt.rcParams['xtick.labelsize']=8plt.rcParams['ytick.labelsize']=8
Finally, if this is a setting that you would like to be set for all your matplotlib plots, you could also set these two rcParams
in your matplotlibrc
file:
xtick.labelsize : 8 # fontsize of the x tick labelsytick.labelsize : 8 # fontsize of the y tick labels