如何在 Matplotlib 中设置图形标题和轴标签字体大小? [英] How do I set the figure title and axes labels font size in Matplotlib?

查看:66
本文介绍了如何在 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?

推荐答案

处理文本的函数,如 labeltitle 等,接受与 matplotlib.text.Text.对于字体大小,您可以使用 size/fontsize:

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

对于全局设置 titlelabel 大小,mpl.rcParams 包含 axes.titlesizeaxes.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

(据我所知,没有办法分别设置 xy 标签大小.)

(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆