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
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS