由于使用的是mac,这几天使用 matplptlib遇到显示中文问题
原设置字体如下:
# 指定默认字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False

原因:
- matplotlib第三方库中没有中文字体包
- 查看matplotlib所有字体:
from matplotlib import font_manager
a = sorted([f.name for f in font_manager.fontManager.ttflist])
for i in a:
print(i)
解决办法:
- 网络下载中文字体包 ttf 格式
- 我的路径/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/tff

- 将下载的对应的ttf文件放入ttf文件夹中
- 重新加载matplotlib的字体库
from matplotlib.font_manager import _rebuild
_rebuild()

搞定收工
> 可以通过上面查看matplotlib字体的步骤 查看有没有加载成功和对应的名字
网友评论