How to Set Tick Labels Font Size in Matplotlib?

Matplotlib is a popular library for data visualization in Python. It provides several methods for adjusting the font size of tick labels in a plot. One way is to use the plt.tick_params() method. This method takes in arguments for axis (‘x’ or ‘y’), which sets the font size of the labels, and labelsize which sets the number of points for the font size. Additionally, the parameters fontname and fontweight can be used to adjust the font type and weight. Other methods such as set_xticklabels() and set_yticklabels() can also be used to adjust the font size of the labels. Examples of setting the font size of the tick labels are provided in the code below.


You can use the following syntax to set the tick labels font size of plots in Matplotlib:

import matplotlib.pyplot as plt

#set tick labels font size for both axes
plt.tick_params(axis='both', which='major', labelsize=20)

#set tick labels font size for x-axis only
plt.tick_params(axis='x', which='major', labelsize=20)

#set tick labels font size for y-axis only
plt.tick_params(axis='y', which='major', labelsize=20)

The following examples show how to use this syntax in practice.

Example 1: Set Tick Labels Font Size for Both Axes

The following code shows how to create a plot using Matplotlib and specify the tick labels font size for both axes:

import matplotlib.pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)

#set tick labels font size for both axes
plt.tick_params(axis='both', which='major', labelsize=20)

#display plot
plt.show()

Notice that we increased both the x-axis and y-axis tick labels font size.

Example 2: Set Tick Labels Font Size for X-Axis Only

The following code shows how to create a plot using Matplotlib and specify the tick labels font size for just the x-axis:

import matplotlib.pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)

#set tick labels font size for both axes
plt.tick_params(axis='x', which='major', labelsize=20)

#display plot
plt.show()

Notice that we increased just the x-axis tick labels font size.

Example 3: Set Tick Labels Font Size for Y-Axis Only

The following code shows how to create a plot using Matplotlib and specify the tick labels font size for just the y-axis:

import matplotlib.pyplot as plt

#define x and y
x = [1, 4, 10]
y = [5, 11, 27]

#create plot of x and y
plt.plot(x, y)

#set tick labels font size for both axes
plt.tick_params(axis='y', which='major', labelsize=20)

#display plot
plt.show()

 

x