如何在Matplotlib中设置图形标题和轴标签的字体大小? [英] How do I set the figure title and axes labels font size in Matplotlib?
问题描述
我正在Matplotlib中创建一个图形,如下所示:
I am creating a figure in Matplotlib like this:
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig('test.jpg')
我想为图形标题和轴标签指定字体大小.我需要所有三种字体大小都不同,所以我不需要设置全局字体大小(mpl.rcParams['font.size']=x
).如何分别设置图形标题和轴标签的字体大小?
I want to specify font sizes for the figure title and the axis labels. I need all three to be different font sizes, so setting a global font size (mpl.rcParams['font.size']=x
) is not what I want. How do I set font sizes for the figure title and the axis labels individually?
推荐答案
Functions dealing with text like label
, title
, etc. accept parameters same as matplotlib.text.Text
. For the font size you can use size/fontsize
:
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title', fontsize=20)
plt.xlabel('xlabel', fontsize=18)
plt.ylabel('ylabel', fontsize=16)
fig.savefig('test.jpg')
对于全局设置title
和label
大小, mpl.rcParams
包含axes.labelsize
. (来自页面):
For globally setting title
and label
sizes, mpl.rcParams
contains axes.titlesize
and axes.labelsize
. (From the page):
axes.titlesize : large # fontsize of the axes title
axes.labelsize : medium # fontsize of the x any y labels
(据我所知,无法单独设置x
和y
标签大小.)
(As far as I can see, there is no way to set x
and y
label sizes separately.)
我发现axes.titlesize
不会影响suptitle
.我想,您需要手动设置.
And I see that axes.titlesize
does not affect suptitle
. I guess, you need to set that manually.
这篇关于如何在Matplotlib中设置图形标题和轴标签的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!