How to Change Legend Font Size in Matplotlib?
Last Updated :
15 Sep, 2022
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.
Using pyplot.legend Change Legend Font Size
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 )
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 )
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 ])
plt.legend([ 'blue' , 'orange' , 'green' , 'red' ],
prop = { 'size' : 20 },
loc = 'upper left' , shadow = True ,
facecolor = 'yellow' )
plt.show()
|
Output:
Using pyplot.rc Change Legend Font Size
The matplotlib.rcparams is a dictionary-like variable that has all the configuration settings to customize default parameters. The matplotlib.rc() command is used to change multiple settings with the use of keyword arguments.
Example:
Python3
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
x = np.linspace( 0 , ( 2 * np.pi), endpoint = True )
xlim(x. min (), x. max ())
xticks([ 0 , np.pi / 2 , np.pi, ( 3 * np.pi / 2 ), ( 2 * np.pi)],
[r '$0$' , r '$+\pi/2$' , r '$+\pi$' , r '$3/2\pi$' , r '$2\pi$' ])
ylim( - 1 , 0 , 1 )
yticks([ - 1 , 0 , + 1 ],
[r '$-1$' , r '$0$' , r '$+1$' ])
plt.plot(x, np.sin(x), label = "sin(x)" )
plt.plot(x, np.cos(x), label = "cos(x)" )
plt.title( 'Trigonometric Functions' , fontsize = 22 )
plt.xlabel( 'Angles' , fontsize = 18 )
plt.ylabel( 'Values' , fontsize = 18 )
plt.legend(loc = 'upper center' )
plt.rc( 'legend' , fontsize = 16 )
plt.tight_layout()
plt.show()
|
Output:
Share your thoughts in the comments
Please Login to comment...