matplotlib 无法显示中文

直接使用本地的 ttc 格式字体源,本例中使用的是 Windows 自带的微软雅黑字体,当然你也可以更换成其他的字体,但是前提是你预先下载好该字体的源文件。

1
2
3
4
5
6
7
8
9
10
11
12
import matplotlib.font_manager as fm
myfont = fm.FontProperties(fname='C:/Windows/Fonts/msyh.ttc')
%matplotlib inline

# 记得在中文前加上 u 字符
import matplotlib.pyplot as plt
plt.title(u'测试中文', fontproperties=myfont)
plt.xlabel(u'测试中文', fontproperties=myfont)
plt.ylabel(u'测试中文', fontproperties=myfont)
plt.legend()
plt.grid(True)
plt.show()

运行上述代码,matplotlib 无法显示中文的问题就解决了。

中文测试成功

------本文结束,欢迎收藏本站、分享、评论或联系作者!------
点击查看
赠人玫瑰 手有余香
0%