How to 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()

Related Article - Matplotlib Axis

  • How to Rotate X-axis Tick Label Text in Matplotlib
  • How to Hide Axis Text Ticks and/or Tick Labels in Matplotlib
  • Related Article - Matplotlib Xticks

  • How to Rotate X-axis Tick Label Text in Matplotlib
  • How to Hide Axis Text Ticks and/or Tick Labels in Matplotlib
  • How to Rotate X-axis Tick Label Text in Matplotlib
  • comments powered by Disqus