红联Linux门户
Linux帮助

完美解决Python下matplotlib绘图中文乱码问题

发布时间:2019-05-23 00:00:00来源:未知作者:admin

当我们用matplotlib作图时中文乱码,往往会发现中文的文字变成了小方块,我在绘制决策树的时候就碰到了这个问题。将各种方式试了个遍,都不管用……最终发现是没有删除matplotlib下字体的缓存目录,即~/.cache/matplotlib

下面记录找到的比较好的方法,以便下次使用。

1、环境查看

a.系统版本查看

linuxidc@linuxidc:~/www.linuxidc.com$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"

b.系统中文字体查看

linuxidc@linuxidc:~/www.linuxidc.com$ fc-list :lang=zh
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/home/linuxidc/.local/share/fonts/方正准圆简体.TTF: 方正准圆简体,FZZhunYuan\-M02S:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP,Noto Sans CJK JP Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK SC:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK TC:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK JP:style=Bold
/usr/share/fonts/opentype/noto/NotoSerifCJK-Bold.ttc: Noto Serif CJK KR:style=Bold
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK KR,Noto Sans Mono CJK KR Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK JP,Noto Sans Mono CJK JP Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK JP,Noto Sans CJK JP Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK SC:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK KR,Noto Sans CJK KR Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK TC:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK JP:style=Regular
/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc: Noto Serif CJK KR:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans Mono CJK SC,Noto Sans Mono CJK SC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK JP,Noto Sans Mono CJK JP Regular:style=Regular
/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK TC,Noto Sans CJK TC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK KR,Noto Sans Mono CJK KR Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK SC,Noto Sans CJK SC Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK SC,Noto Sans CJK SC Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK TC,Noto Sans CJK TC Regular:style=Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW MBE:style=Book
/home/linuxidc/.local/share/fonts/SIMHEI.TTF: 黑体,SimHei:style=Regular,Normal,oby?ejné,Standard,Κανονικ?,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Regular:style=Regular
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing CN:style=Light
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans Mono CJK SC,Noto Sans Mono CJK SC Regular:style=Regular
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing HK:style=Light
/home/linuxidc/.local/share/fonts/文泉驿正黑.ttf: 文泉驿正黑,文泉驛正黑,WenQuanYi Zen Hei:style=Medium,中等
/home/linuxidc/.local/share/fonts/方正水柱_GBK.ttf: 方正水柱_GBK,FZShuiZhu\-M08:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK KR,Noto Sans CJK KR Bold:style=Bold,Regular

c.Python版本及matplotlib配置文件位置查询

linuxidc@linuxidc:~/www.linuxidc.com$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc'
>>>

2、第一种解决方法

在代码中指定字体配置

#coding:utf-8 
import matplotlib 
matplotlib.use('qt4agg') 
from matplotlib.font_manager import * 
import matplotlib.pyplot as plt 
#定义自定义字体,文件名从1.b查看系统中文字体中来 
myfont = FontProperties(fname='/home/linuxidc/.local/share/fonts/文泉驿正黑.ttf') 
#解决负号'-'显示为方块的问题 
matplotlib.rcParams['axes.unicode_minus']=False 
plt.plot([-1,2,-5,3]) 
plt.title(u'Linux公社的网址是www.linuxidc.com',fontproperties=myfont) 
plt.show()

3.第二种解决办法

首先将Windwos中fonts目录下的simhei.ttf拷贝到  /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf  (文件路径参考1.c,根据实际情况修改)目录中,

然后删除~/.cache/matplotlib的缓冲目录

第三在代码中动态设置参数:

#coding:utf-8 
import matplotlib 
matplotlib.use('qt4agg') 
#指定默认字体 
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] 
matplotlib.rcParams['font.family']='sans-serif' 
#解决负号'-'显示为方块的问题 
matplotlib.rcParams['axes.unicode_minus'] = False 
plt.plot([-1,2,-5,3]) 
plt.title(u'Linux公社的网址是www.linuxidc.com',fontproperties=myfont) 
plt.show()

4.第三种解决办法
首先将Windwos中fonts目录下的simhei.ttf拷贝到 /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf  目录中,
然后删除~/.cache/matplotlib的缓冲目录

第三修改修改配置文件:
linuxidc@linuxidc:~$ vi /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data//matplotlibrc

文件路径参考1.c,根据实际情况修改,找到如下两项,去掉前面的#,并在font.sans-serif冒号后面加上SimHei,保持退出。

font.family        : sans-serif       
font.sans-serif    : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

就是知道字库族为sans-serif,同时添加“SimHei”即宋体到字库族列表中,同时将找到axes.unicode_minus,将True改为False,作用就是解决负号'-'显示为方块的问题

5.常见问题

a.当matplotlib/mpl-data/fonts/ttf中没有指定字体是执行时会出现如下错误.

font_manager.py:1287: UserWarning: findfont: Font family [u'sans-serif'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

b.有字体但还是显示小方块,一般是没有删除~/.cache/matplotlib 的缓冲目录!

c.matplotlib.use(‘qt4agg’)出错,plt.show()没显示。

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17