方法一
# 这里的 `Microsoft YaHei` 可以换成其它的中文字体
# plt.xlabel/ylabel/title 中的中文标签会自动显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
方法二
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc")
plt.title('中文标题', fontproperties=font_set)
# `fontproperties=font_set` 同样的方式设置在 xlabel/ylabel 里
- 一般的,电脑字体都存放在C: \Windows\Fonts中。

本文介绍两种在Matplotlib中实现中文标签显示的方法:一是通过修改默认的sans-serif字体为系统中存在的中文字体,如Microsoft YaHei;二是利用FontManager加载指定的中文字体文件,并在设置标题、坐标轴标签时指定该字体。
1029

被折叠的 条评论
为什么被折叠?



