Skip to content
Related Articles

Related Articles

How to change the size of axis labels in Matplotlib?
  • Last Updated : 03 Jan, 2021

Matplotlib is a great data plotting tool. It’s used for visualizing data and also for presenting the data to your team on a presentation or for yourself for future reference. So, while presenting it might happen that the “X-label” and “y-label” are not that visible and for that reason, we might want to change its font size.  So in this article, we are going to see how we can change the size of axis labels in Matplotlib. 
 

Before starting let’s draw a simple plot with matplotlib.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output 



Now we will see how to change the size of the axis labels:

Example 1: Changing both axis label.

 If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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 = 12)
ax.set_ylabel('y-axis', fontsize = 10)
  
plt.show()

chevron_right


Output 

Example 2: Changing the y-axis label.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
  
plt.xticks(fontsize = 15)
plt.show()

chevron_right


Output

Here the font size of X values is being set to 15


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :