Thursday, June 11, 2026
HomeLanguagesHow to set font size of Matplotlib axis Legend?

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

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:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS