解决python Matplotlib绘图中文字体乱码问题_centos

在使用python的matplotlib画图时,中文显示乱码,程序中使用的代码如下,

matplotlib.rcParams['font.family'] = 'SimHei' 

执行之后程序报如下的错误,并且画出来的图中文显示乱码。

findfont: Font family ['SimHei'] not found. Falling back to DejaVu Sans.
/usr/local/lib64/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 20154 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/usr/local/lib64/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 20687 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/usr/local/lib64/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 22812 missing from current font.
  font.set_text(s, 0.0, flags=flags)
/usr/local/lib64/python3.6/site-packages/matplotlib/backends/backend_agg.py:238: RuntimeWarning: Glyph 26223 missing from current font.
  font.set_text(s, 0.0, flags=flags)

从错误提示可以看到是缺少字体,于是使用如下的命令 fc-list :lang=zh得到现在系统中有那些中文字体,结果如下

[root@222 chw]# fc-list :lang=zh
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Sharp,文泉驛點陣正黑:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/chinese/simhei.ttf: SimHei:style=Regular,Normal
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: WenQuanYi Zen Hei Mono:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing HK:style=Light
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing CN:style=Light
/usr/share/fonts/yifont/mullerfont.ttf: WenQuanYi Zen Hei Mono:style=Medium

发现系统是有中文字体的,只不过没有SimHei,于是我把python语句中设置字体的那一行改成下面这一行就可以了,

matplotlib.rcParams['font.family'] = 'WenQuanYi Micro Hei' 

但是上面的方法其实是我在安装SimHei字体失败之后使用的方法,虽然问题解决了,但是SimHei字体并没有安装成功,还是忍不住想把字体安装成功,

你可能感兴趣的:(Ubuntu/CentOS使用,python)