Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to Set Tick Labels Font Size in Matplotlib?

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:

  • Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
  • Tick Label : Ticks are the markers denoting data points on axes. Matplotlib’s default tick locators and formatters are designed to be generally sufficient in many common situations. Position and labels of ticks are often explicitly mentioned to suit specific requirements.
  • Font Size : The font size or text size is how large the characters displayed on a screen or printed on a page are.

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:

  • fontsize in  plt.xticks/plt.yticks()
  • fontsize in  ax.set_yticklabels/ax.set_xticklabels()
  • labelsize in  ax.tick_params()

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

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

Python3




# 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)

Python3




# 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)

Python3




# 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:

RELATED ARTICLES

Most Popular

Recent Comments