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

时间:2022-05-02 23:42:20

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. How do I set font sizes for the figure title and the axis labels individually?

我想为图标题和轴标签指定字体大小。我需要这三种字体大小都不同,所以设置全局字体大小(mpl.rcParams['font.size']]=x)不是我想要的。如何分别设置图标题和轴标签的字体大小?

3 个解决方案

#1


449  

Functions dealing with text like label, title, etc. accept parameters same as matplotlib.text.Text. For the font size you can use size/fontsize:

处理文本(如标签、标题等)的函数接受与matplotlib.text.Text相同的参数。对于字体大小,您可以使用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. (From the page):

用于全局设置标题和标签大小,mpl。rcParams包含轴。titlesize axes.labelsize。(从页面):

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

(在我看来,没有办法分别设置x和y的标签尺寸。)

And I see that axes.titlesize does not affect suptitle. I guess, you need to set that manually.

我看到坐标轴。titlesize不影响suptitle。我想你需要手动设置。

#2


36  

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)

#3


16  

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. It seems to need a redraw operation after to see the effect. For example:

如果您更习惯于使用ax对象进行绘图,您可能会发现ax.xaxis.label.set_size()更容易记住,或者至少更容易在ipython终端中找到使用tab的方法。似乎需要重新绘制之后才能看到效果。例如:

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.

我不知道有什么类似的方法可以在创建后设置假设大小。

#1


449  

Functions dealing with text like label, title, etc. accept parameters same as matplotlib.text.Text. For the font size you can use size/fontsize:

处理文本(如标签、标题等)的函数接受与matplotlib.text.Text相同的参数。对于字体大小,您可以使用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. (From the page):

用于全局设置标题和标签大小,mpl。rcParams包含轴。titlesize axes.labelsize。(从页面):

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

(在我看来,没有办法分别设置x和y的标签尺寸。)

And I see that axes.titlesize does not affect suptitle. I guess, you need to set that manually.

我看到坐标轴。titlesize不影响suptitle。我想你需要手动设置。

#2


36  

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)

#3


16  

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. It seems to need a redraw operation after to see the effect. For example:

如果您更习惯于使用ax对象进行绘图,您可能会发现ax.xaxis.label.set_size()更容易记住,或者至少更容易在ipython终端中找到使用tab的方法。似乎需要重新绘制之后才能看到效果。例如:

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.

我不知道有什么类似的方法可以在创建后设置假设大小。