如何更改 matplotlib 图上的字体大小

新手上路,请多包涵

如何更改 matplotlib 图上所有元素(刻度、标签、标题)的字体大小?

我知道如何更改刻度标签大小,这是通过以下方式完成的:

 import matplotlib
matplotlib.rc('xtick', labelsize=20)
matplotlib.rc('ytick', labelsize=20)

但是如何改变其余的呢?

原文由 Herman Schaaf 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 946
2 个回答

matplotlib 文档 中,

 font = {'family' : 'normal',
        'weight' : 'bold',
        'size'   : 22}

matplotlib.rc('font', **font)

这会将所有项目的字体设置为 kwargs 对象指定的字体 font

或者,您也可以使用 rcParams update 此答案 中建议的方法:

 matplotlib.rcParams.update({'font.size': 22})

或者

import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 22})

您可以在 自定义 matplotlib 页面 上找到可用属性的完整列表。

原文由 Herman Schaaf 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果你像我一样是个控制狂,你可能想要明确地设置你所有的字体大小:

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

原文由 Pedro M Duarte 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题