How to use pyplot.legend Change Legend Font Size In Matplotlib

Example 1: using fontsize

Here, we are trying to change the font size of the x and y labels.

Python3




import matplotlib.pyplot as plt
import numpy as np
 
plt.figure(figsize = (8, 4))
 
x = ['Arjun', 'Bharath', 'Raju', 'Seeta', 'Ram']
y = [5, 7, 8, 4, 6]
 
plt.bar(x, y, color = 'g')
 
plt.xlabel('Students', fontsize = 18)
plt.ylabel('Marks', fontsize = 18)
 
#Default fontsize of text using legend
plt.legend(['Marks scored'])
 
plt.show()


Output:

 

Example 2: Changing text font size

This example changes the font size of items in the legend. The font size parameter can have integer or float values. It also accepts the string sizes like: ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’.

Python3




import matplotlib.pyplot as plt
import numpy as np
 
plt.figure(figsize = (8, 4))
 
x = ['Arjun', 'Bharath', 'Raju', 'Seeta', 'Ram']
 
y = [5, 7, 8, 4, 6]
 
plt.bar(x, y, color = 'g')
 
plt.xlabel('Students', fontsize = 18)
plt.ylabel('Marks', fontsize = 18)
 
#Changing text fontsize in legend
plt.legend(['Marks scored'], fontsize = 17)
 
plt.show()


Output: 

 

Example 3: Using prop keyword

The prop keyword is used to change the font size property. It is used in Matplotlib as Using a prop keyword for changing the font size in legend.

Python3




import matplotlib.pyplot as plt
 
plt.figure(figsize = (8 , 5))
 
plt.plot([1, 2, 4, 8, 30, 1])
plt.plot([1, 6, 13, 20, 38, 1])
plt.plot([1, 4, 8, 14, 20, 1])
plt.plot([1, 3, 4, 5, 10, 1])
 
#Using prop keyword in legend to change font size
plt.legend(['blue', 'orange', 'green', 'red'],
           prop = {'size' : 20},
           loc = 'upper left', shadow = True,
           facecolor = 'yellow')
 
plt.show()


Output:

 

How to Change Legend Font Size in Matplotlib?

Matplotlib is a library for creating interactive Data visualizations in Python. The functions in Matplotlib make it work like MATLAB software. The legend method in Matplotlib describes the elements in the plot. In this article, we are going to Change Legend Font Size in Matplotlib.

Similar Reads

Using pyplot.legend Change Legend Font Size

Example 1: using fontsize...

Using pyplot.rc Change Legend Font Size

...

Contact Us