Set Tick Labels Font Size in Matplotlib

Jinku Hu Jan 30, 2023 Dec 11, 2019
  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
Set Tick Labels Font Size in Matplotlib

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()
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

Related Article - Matplotlib Axes

Related Article - Matplotlib Ticks