Open In App

How to set font size of Matplotlib axis Legend?

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Matplotlib

In this article, we will see how to set the font size of matplotlib axis legend using Python. For this, we will use rcParams() methods to increase/decrease the font size. To use this we have to override the matplotlib.rcParams[‘legend.fontsize’] method.

Syntax: matplotlib.rcParams[‘legend.fontsize’] = font_size_value

Example 1: Setting plot size of the legend is 15

Python3




# import required modules
import numpy
from matplotlib import pyplot
import matplotlib
  
# assign value to x axis
x_axis = numpy.arange(1, 20, 0.5)
  
# get the value of log10
y_axis_log10 = numpy.exp(x_axis)
  
# plot the graph
pyplot.plot(x_axis, y_axis_log10, c="blue",
            label="exponential")
  
# title of the graph/plot
pyplot.title("Exponential Graph Plot")
  
# to set the font size of the legend
matplotlib.rcParams['legend.fontsize'] = 15
  
pyplot.legend(loc='best')
  
pyplot.show()


Output:

Example 2: Setting the plot size of the legend is 25

Python3




# import required modules
import numpy
from matplotlib import pyplot
import matplotlib
  
# assign value to x axis
x_axis = numpy.arange(1, 20, 0.5)
  
# get the value of log10
y_axis_log10 = numpy.exp(x_axis)
  
# plot the graph
pyplot.plot(x_axis, y_axis_log10, c="blue", label="exponential")
  
# Title of the graph/ploy
pyplot.title("Exponential Graph Plot")
  
# to set the font size of the legend
matplotlib.rcParams['legend.fontsize'] = 25
  
pyplot.legend(loc='best')
  
pyplot.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads