matplotlib 显示中文

matplotlib 显示中文

Method_1:

# 添上:
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

Method_2:

先定义myfont变量, 使其获取某个本地的字体, 然后在需要显示相应字体的函数中使用fontproperties参数, 赋值myfont即可:

from matplotlib.font_manager import FontProperties
myfont = FontProperties(fname='/usr/share/fonts/truetype/simhei.ttf', size=20)
...
plt.title(SOME_STR, fontproperties=myfont)

Ubuntu中缺少中文字体:

网上下

字体下载网站
下载SimHei入口1
下载SimHei入口2

这里下载下来的是.rar文件, 若系统无rar和unrar, 则以apt-get下载之, 之后将其解压:
rar+unrar使用参考链接

unrar x simheittf.rar

搬运windows中的

C:/Windows/fonts/里面挑选

放至Ubuntu中的路径

/usr/share/fonts/truetype

原文地址:https://www.cnblogs.com/ZhengPeng7/p/8823956.html