如何更改matplotlib图上所有元素(刻度、标签、标题)的字体大小?
我知道如何更改刻度标签的大小,这可以通过以下方式完成:
import matplotlib
matplotlib.rc('xtick', labelsize=20)
matplotlib.rc('ytick', labelsize=20)
但是如何改变剩下的呢?
发布于 2010-10-10 19:49:11
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 22}
matplotlib.rc('font', **font)
这会将所有项目的字体设置为由kwargs对象font
指定的字体。
或者,您也可以按照this answer中的建议使用rcParams
update
方法
matplotlib.rcParams.update({'font.size': 22})
或
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 22})
您可以在Customizing matplotlib page上找到可用属性的完整列表。
发布于 2016-09-19 13:39:01
如果你像我一样是一个控制狂,你可能想要显式地设置所有的字体大小:
import matplotlib.pyplot as plt
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
请注意,还可以在matplotlib
上调用rc
方法来设置大小
import matplotlib
SMALL_SIZE = 8
matplotlib.rc('font', size=SMALL_SIZE)
matplotlib.rc('axes', titlesize=SMALL_SIZE)
# and so on ...
发布于 2013-02-20 10:13:33
如果只想更改已创建的特定绘图的字体大小,请尝试以下操作:
import matplotlib.pyplot as plt
ax = plt.subplot(111, xlabel='x', ylabel='y', title='title')
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(20)
https://stackoverflow.com/questions/3899980
复制相似问题