Open In App

How to set font size of Matplotlib axis Legend?

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




# 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




# 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:


Article Tags :