49

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
FaCoffee
  • 7,609
  • 28
  • 99
  • 174

4 Answers4

71

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 plt

plt.rc('xtick',labelsize=8)
plt.rc('ytick',labelsize=8)

Or, equivalently:

plt.rcParams['xtick.labelsize']=8
plt.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 labels
ytick.labelsize      : 8 # fontsize of the y tick labels
tmdavison
  • 64,360
  • 12
  • 187
  • 165
52

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.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • this worked great for jupyter/pandas. for example i was looking to change font sizes for code like this: `fig, ax = plt.subplots(figsize=(12,8)) df['metric'].plot(kind='bar',ax=ax)` – measureallthethings Jul 09 '19 at 17:40
  • I does not work for me. Check out here: `import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame({ 'sales': [3, 2, 3, 9, 10, 6], 'signups': [5, 5, 6, 12, 14, 13], 'visits': [20, 42, 28, 62, 81, 50], }, index=pd.date_range(start='2018/01/01', end='2018/07/01', freq='M')) ax = df.plot.area() plt.tick_params(axis="x", labelsize=18) plt.tick_params(axis="y", labelsize=18) plt.show()` – Carlo Bianchi Feb 25 '20 at 19:07
  • 1
    @CarloBianchi It does work. If you want to change the size of the minor ticklabels, add `which="both"`. – ImportanceOfBeingErnest Feb 25 '20 at 19:42
2

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

FaCoffee
  • 7,609
  • 28
  • 99
  • 174
1

Use the keyword size instead of fontsize.

toti08
  • 2,448
  • 5
  • 24
  • 36
  • However according to the docs (http://matplotlib.org/users/text_props.html) your code should also work. – toti08 Nov 30 '15 at 15:39