Set Tick Labels Font Size in Matplotlib

  1. plt.xticks(fontsize= ) to Set Matplotlib Tick Labels Font Size
  2. ax.set_xticklabels(xlabels, Fontsize= ) to Set Matplotlib Tick Labels Font Size
  3. plt.setp(ax.get_xticklabels(), Fontsize=) to Set Matplotlib Tick Labels Font Size
  4. ax.tick_params(axis='x', Labelsize= ) to Set Matplotlib Tick Labels Font Size

In this tutorial article, we will introduce different methods to set tick labels font size in Matplotlib. It includes,

  • plt.xticks(fontsize= )
  • ax.set_xticklabels(xlabels, fontsize= )
  • plt.setp(ax.get_xticklabels(), fontsize=)
  • ax.tick_params(axis='x', labelsize= )

We will use the same data set in the following code examples.

Matplotlib set tick labels font size_basic

The codes to create the above figure is,

from matplotlib import pyplot as plt
from datetime import datetime, timedelta

xvalues = range(10)
yvalues = xvalues

fig,ax = plt.subplots()
plt.plot(xvalues, yvalues)
plt.grid(True)

plt.show()

plt.xticks(fontsize= ) to Set Matplotlib Tick Labels Font Size

from matplotlib import pyplot as plt
from datetime import datetime, timedelta

xvalues = range(10)
yvalues = xvalues

fig,ax = plt.subplots()
plt.plot(xvalues, yvalues)
plt.xticks(fontsize=16)
plt.grid(True)

plt.show()
plt.xticks(fontsize=16)

plt.xticks gets or sets the properties of tick locations and labels of the x-axis.

fontsize or size is the property of a Text instance, and can be used to set the font size of tick labels.

Matplotlib set tick labels font size_xticks

ax.set_xticklabels(xlabels, Fontsize= ) to Set Matplotlib Tick Labels Font Size

set_xticklabels sets the x-tick labels with a list of string labels, with the Text properties as the keyword arguments. Here, fontsize sets the tick labels font size.

from matplotlib import pyplot as plt
from datetime import datetime, timedelta
import numpy as np

xvalues = np.arange(10)
yvalues = xvalues

fig,ax = plt.subplots()
plt.plot(xvalues, yvalues)
plt.xticks(xvalues)
ax.set_xticklabels(xvalues, fontsize=16)
plt.grid(True)

plt.show()

plt.setp(ax.get_xticklabels(), Fontsize=) to Set Matplotlib Tick Labels Font Size

matplotlib.pyplot.setp sets a property on an artist object.

plt.setp(ax.get_xticklabels(), fontsize=) sets the fontsize property of xtick labels object.

from matplotlib import pyplot as plt
from datetime import datetime, timedelta

xvalues = np.arange(10)
yvalues = xvalues

fig,ax = plt.subplots()
plt.plot(xvalues, yvalues)
plt.setp(ax.get_xticklabels(), fontsize=16)

plt.grid(True)

plt.show()

ax.tick_params(axis='x', Labelsize= ) to Set Matplotlib Tick Labels Font Size

tick_params sets the parameters of ticks, tick labels, and gridlines.

ax.tick_params(axis='x', labelsize= ) sets the labelsize property of tick label in x axis, or in other words, X-axis.

from matplotlib import pyplot as plt
from datetime import datetime, timedelta

xvalues = range(10)
yvalues = xvalues

fig,ax = plt.subplots()
plt.plot(xvalues, yvalues)
ax.tick_params(axis='x', labelsize=16)
plt.grid(True)

plt.show()
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Matplotlib Axes

  • Rotate X-Axis Tick Label Text in Matplotlib
  • Differences Between cla(), clf() and close() Methods in Matplotlib
  • Related Article - Matplotlib Ticks

  • Turn Off the Axes for Subplots in Matplotlib