当我在matplotlib中制作图形时,我通常希望将所有格式匹配的文本(例如,轴标签,图例,轴刻度)以粗体和大小20表示。有一种方法可以在一个命令中执行此操作不必在每个文本实例上都指定它?
我正在寻找的东西将会改变这种情况:
fig, ax = plt.figure()
ax.set_title("Title", fontsize = 20, fontweight = 'bold')
plt.xlabel("xlabel", fontsize = 20, fontweight = 'bold')
plt.ylabel("ylabel", fontsize = 20, fontweight = 'bold')
plt.xticks(fontsize = 20, fontweight = 'bold')
plt.yticks(fontsize = 20, fontweight = 'bold')
更像这样的事情,而不影响输出:
fig, ax = plt.figure()
alltextformat = fontsize = 20, fontweight = 'bold'
ax.set_title("Title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.xticks()
plt.yticks()
答案 0 :(得分:1)
是的,您想更改rcParams
。一种方法是这样的:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10)
y = x ** 2
with plt.rc_context(rc={'font.size': 20, 'font.weight': 'bold'}):
plt.plot(x, y)
plt.show()
或整个会话
plt.rc('font', size=20, weight='bold')
plt.plot(x, y)
plt.show()
另请参阅:https://matplotlib.org/3.1.1/tutorials/introductory/customizing.html