如何在Matplotlib中设置图标题和轴标签字体大小?

在Matplotlib中,可以通过为`matplotlib.text.Text`对象设置参数来分别调整图标题和轴标签的字体大小。全局设置可通过rcParams字典完成,但要单独改变已创建标题的字体大小,需使用特定方法。另外,也可以通过调整垫片来改变字体视觉大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How do I set the figure title and axes labels font size in Matplotlib?

I am creating a figure in Matplotlib like this: 我正在Matplotlib中创建一个像这样的人物:

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')

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. 我需要三个不同的字体大小,所以设置全局字体大小( mpl.rcParams['font.size']=x )不是我想要的。 How do I set font sizes for the figure title and the axis labels individually? 如何单独设置图标题和轴标签的字体大小?


#1楼

参考:https://stackoom.com/question/qDRE/如何在Matplotlib中设置图标题和轴标签字体大小


#2楼

Functions dealing with text like label , title , etc. accept parameters same as matplotlib.text.Text . 处理labeltitle等文本的函数接受与matplotlib.text.Text相同的参数。 For the font size you can use size/fontsize : 对于字体大小,您可以使用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')

For globally setting title and label sizes, mpl.rcParams contains axes.titlesize and axes.labelsize . 对于全局设置titlelabel大小, mpl.rcParams包含axes.titlesizeaxes.labelsize (From the page): (来自页面):

axes.titlesize      : large   # fontsize of the axes title
axes.labelsize      : medium  # fontsize of the x any y labels

(As far as I can see, there is no way to set x and y label sizes separately.) (据我所知,没有办法单独设置xy标签尺寸。)

And I see that axes.titlesize does not affect suptitle . 我看到axes.titlesize不会影响suptitle I guess, you need to set that manually. 我想,你需要手动设置。


#3楼

You can also do this globally via a rcParams dictionary: 您也可以通过rcParams字典全局执行此操作:

import matplotlib.pylab as pylab
params = {'legend.fontsize': 'x-large',
          'figure.figsize': (15, 5),
         'axes.labelsize': 'x-large',
         'axes.titlesize':'x-large',
         'xtick.labelsize':'x-large',
         'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)

#4楼

If you're more used to using ax objects to do your plotting, you might find the ax.xaxis.label.set_size() easier to remember, or at least easier to find using tab in an ipython terminal. 如果您更习惯使用ax对象进行绘图,则可能会发现ax.xaxis.label.set_size()更容易记住,或者至少更容易使用ipython终端中的tab找到。 It seems to need a redraw operation after to see the effect. 看来效果似乎需要重绘操作。 For example: 例如:

import matplotlib.pyplot as plt

# set up a plot with dummy data
fig, ax = plt.subplots()
x = [0, 1, 2]
y = [0, 3, 9]
ax.plot(x,y)

# title and labels, setting initial sizes
fig.suptitle('test title', fontsize=12)
ax.set_xlabel('xlabel', fontsize=10)
ax.set_ylabel('ylabel', fontsize='medium')   # relative to plt.rcParams['font.size']

# setting label sizes after creation
ax.xaxis.label.set_size(20)
plt.draw()

I don't know of a similar way to set the suptitle size after it's created. 我不知道在创建之后设置suptitle大小的类似方法。


#5楼

To only modify the title's font (and not the font of the axis) I used this: 要仅修改标题的字体(而不是轴的字体),我使用了这个:

import matplotlib.pyplot as plt
fig = plt.Figure()
ax = fig.add_subplot(111)
ax.set_title('My Title', fontdict={'fontsize': 8, 'fontweight': 'medium'})

The fontdict except all kwargs from matplotlib.text.Text . 除了matplotlib.text.Text中的所有kwargs之外的fontdict。


#6楼

An alternative solution to changing the font size is to change the padding. 更改字体大小的替代解决方案是更改填充。 When Python saves your PNG, you can change the layout using the dialogue box that opens. 当Python保存您的PNG时,您可以使用打开的对话框更改布局。 The spacing between the axes, padding if you like can be altered at this stage. 如果您愿意,可以在此阶段更改轴之间的间距,填充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值