Open In App

How to change the size of axis labels in Matplotlib?

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Matplotlib is a Python library that helps in visualizing and customizing various plots. One of the customization you can do is to change the size of the axis labels to make reading easier. In this guide, we’ll look how to adjust font size of axis labels using Matplotlib.

Let’s start with a basic plot to see how it works

Python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [9, 8, 7, 6, 5]

fig, ax = plt.subplots()
ax.plot(x, y)

ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')

plt.show()

Output 

download6

Sample Plot

1. Using xlabel() and ylabel()

We can change the size of axis labels by using the fontsize parameter in the xlabel() and ylabel() functions. These method allow you to specify the font size directly when setting the labels.

Python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [9, 8, 7, 6, 5]

fig, ax = plt.subplots()
ax.plot(x, y)
ax.plot(x, y)

ax.set_xlabel('x-axis', fontsize = 25)
ax.set_ylabel('y-axis', fontsize = 20)

plt.show()

Output:

Screenshot-2024-12-12-121427

Resulting plot after adjusting axis label size in Matplotlib

Here we defined x-axis font as 25 and y-axis font to be 20.

2. Using set_xlabel() and set_ylabel()

After plotting you can use set_xlabel() and set_ylabel() methods of the Axes object. These methods are used to modify labels after your plot has already been created.

Python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

plt.plot(x, y)

plt.gca().set_xlabel('Custom X-Axis Label', fontsize=20)
plt.gca().set_ylabel('Custom Y-Axis Label', fontsize=20)

plt.show()

Output:

Screenshot-2024-12-12-123717

Resulting output after adjusting axis label size in Matplotlib

3. Changing Font Size Globally with rcParams

If you require consistent font sizes across multiple plots adjusting the default settings with rcParams is a better approach. This method sets a global font size for all axis labels in your environment.

Python
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['axes.labelsize'] = 20

x = [5, 10, 4, 2, 8]
y = [49, 50, 90, 43, 87]

plt.plot(x, y)

plt.xlabel('Custom X-Axis Label')
plt.ylabel('Custom Y-Axis Label')

plt.show()

Output:

Screenshot-2024-12-12-124140

Resulting output after adjusting axis label size in Matplotlib

Adjusting size of axis labels improves readability and understanding. Whether it’s about sharing your plots with someone or just understanding the data alone proper label size makes your visuals better. Using the given ways you will can get neat, professional and clear charts.



Next Article
Practice Tags :

Similar Reads