如何设置 Matplotlib 轴图例的字体大小?
先决条件: Matplotlib
在本文中,我们将看到如何使用Python设置 matplotlib 轴图例的字体大小。为此,我们将使用 rcParams() 方法来增加/减小字体大小。要使用它,我们必须覆盖 matplotlib.rcParams['legend.fontsize'] 方法。
Syntax: matplotlib.rcParams[‘legend.fontsize’] = font_size_value
示例 1:设置图例的绘图大小为 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()
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()
输出:
示例 2:设置图例的绘图大小为 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()
输出: