Open In App

How to Set Tick Labels Font Size in Matplotlib?

Prerequisite: Matplotlib

In this article, we will learn how to change (increase/decrease) the font size of tick label of a plot in matplotlib. For this understanding of following concepts is mandatory:



Approach: To change the font size of tick labels, one should follow some basic steps that are given below:

  1. Import Libraries.
  2. Create or import data.
  3. Plot a graph on data using matplotlib.
  4. Change the font size of tick labels. (this can be done by different methods)

To change the font size of tick labels, any of three different methods in contrast with the above mentioned steps can be employed. These three methods are:



Lets discuss implementation of these methods one by one with the help of examples:

Example 1: (Using plt.xticks/plt.yticks)




# importing libraries
import matplotlib.pyplot as plt
 
# creating data
x = list(range(1, 11, 1))
y = [s*s for s in x]
 
# plotting data
plt.plot(x, y)
 
# changing the fontsize of yticks
plt.yticks(fontsize=20)
 
# showing the plot
plt.show()

Output :

Example 2: (Using ax.set_yticklabels/ax.set_xticklabels)




# importing libraries
import matplotlib.pyplot as plt
import numpy as np
 
# create data
x = list(range(1, 11, 1))
y = np.log(x)
 
# make objects of subplots
fig, ax = plt.subplots()
 
# plot the data
ax.plot(x, y)
 
# change the fontsize
ax.set_xticklabels(x, fontsize=20)
 
# show the plot
plt.show()

Output :

Example 3: (Using ax.tick_params)




# importing libraries
import matplotlib.pyplot as plt
import numpy as np
 
# create data
x = list(range(1, 11, 1))
y = np.sin(x)
 
# make objects of subplots
fig, ax = plt.subplots(1, 1)
 
# plot the data
ax.plot(x, y)
 
# change the fontsize
ax.tick_params(axis='x', labelsize=20)
 
# show the plot
plt.show()

Output:


Article Tags :