彻底解决matplotlib中文乱码问题

28 篇文章 0 订阅
18 篇文章 0 订阅

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

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

1.环境查看

a.系统版本查看

vickyleexy@vickyleexy-pc:~$ cat /etc/lsb-release
DISTRIB_ID=Deepin
DISTRIB_RELEASE=15.4
DISTRIB_DESCRIPTION="Deepin 15.4 "
DISTRIB_CODENAME=unstable

b.系统中文字体查看

vickyleexy@vickyleexy-pc:~$ fc-list :lang=zh
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK JP,Noto Sans CJK JP Bold:style=Bold,Regular
/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/adobe-source-han-sans/SourceHanSansSC-Light.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Light,思源黑体 Light:style=Light,Regular
/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-Black.ttc: Noto Sans CJK TC,Noto Sans CJK TC Black:style=Black,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK KR,Noto Sans CJK KR Black:style=Black,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-Heavy.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Heavy,思源黑体 Heavy:style=Heavy,Regular
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: 文泉驿微米黑,文泉驛微米黑,WenQuanYi Micro Hei:style=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-Medium.ttc: Noto Sans CJK JP,Noto Sans CJK JP Medium:style=Medium,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-Light.ttc: Noto Sans CJK KR,Noto Sans CJK KR Light:style=Light,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-Medium.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Medium,思源黑体 Medium:style=Medium,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK SC,Noto Sans CJK SC Black:style=Black,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/NotoSansCJK-Regular.ttc: Noto Sans CJK KR,Noto Sans CJK KR Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc: Noto Sans CJK SC,Noto Sans CJK SC Light:style=Light,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-Regular.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Regular,思源黑体 Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc: Noto Sans CJK KR,Noto Sans CJK KR Medium:style=Medium,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-DemiLight.ttc: Noto Sans CJK JP,Noto Sans CJK JP DemiLight:style=DemiLight,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Thin.ttc: Noto Sans CJK JP,Noto Sans CJK JP Thin:style=Thin,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-ExtraLight.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC ExtraLight,思源黑体 ExtraLight:style=ExtraLight,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc: Noto Sans CJK JP,Noto Sans CJK JP Light:style=Light,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Thin.ttc: Noto Sans CJK KR,Noto Sans CJK KR Thin:style=Thin,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-Thin.ttc: Noto Sans CJK SC,Noto Sans CJK SC Thin:style=Thin,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK SC,Noto Sans CJK SC Bold:style=Bold,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-Normal.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Normal,思源黑体 Normal:style=Normal,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-DemiLight.ttc: Noto Sans CJK TC,Noto Sans CJK TC DemiLight:style=DemiLight,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-DemiLight.ttc: Noto Sans CJK SC,Noto Sans CJK SC DemiLight:style=DemiLight,Regular
/usr/share/fonts/adobe-source-han-sans/SourceHanSansSC-Bold.otf: 思源黑体,Source Han Sans SC,Source Han Sans SC Bold,思源黑体 Bold:style=Bold,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc: Noto Sans CJK TC,Noto Sans CJK TC Medium:style=Medium,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Black.ttc: Noto Sans CJK JP,Noto Sans CJK JP Black:style=Black,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Medium.ttc: Noto Sans CJK SC,Noto Sans CJK SC Medium:style=Medium,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc: Noto Sans CJK TC,Noto Sans CJK TC Regular:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-DemiLight.ttc: Noto Sans CJK KR,Noto Sans CJK KR DemiLight:style=DemiLight,Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW MBE:style=Book
/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/simsun.ttc: 宋体,SimSun:style=Regular
/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
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: 文泉驿等宽微米黑,文泉驛等寬微米黑,WenQuanYi Micro Hei Mono:style=Regular
/usr/share/fonts/truetype/arphic/simsun.ttc: 新宋体,NSimSun:style=Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Thin.ttc: Noto Sans CJK TC,Noto Sans CJK TC Thin:style=Thin,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc: Noto Sans CJK TC,Noto Sans CJK TC Light:style=Light,Regular
/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc: Noto Sans CJK KR,Noto Sans CJK KR Bold:style=Bold,Regular

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

vickyleexy@vickyleexy-pc:~$ python
Python 2.7.12+ (default, Aug  4 2016, 20:04:34) 
[GCC 6.1.1 20160724] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
u'/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='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc')  
#解决负号'-'显示为方块的问题  
matplotlib.rcParams['axes.unicode_minus']=False  
plt.plot([-1,2,-5,3])  
plt.title(u'中文',fontproperties=myfont)  
plt.show()

3.第二种解决办法

首先将windwos中fonts目录下的simhei.ttf拷贝到/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-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'中文',fontproperties=myfont)  
plt.show()

4.第三钟解决办法

首先将windwos中fonts目录下的simhei.ttf拷贝到/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf目录中,
然后删除~/.cache/matplotlib的缓冲目录
第三修改修改配置文件:
[hadoop@p168 ~]$vim /home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-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()没显示.

转载于:http://blog.csdn.net/dgatiger/article/details/50414549

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于matplotlib中文问题,可以按照以下步骤进行解决: 1. 安装字体库:首先,您可以下载并安装支持中文的字体库,比如SimHei或者Microsoft YaHei。可以从官方网站或者其他可信来源下载字体文件,并将其安装到系统中。 2. 配置matplotlib:接下来,您需要在代码中配置matplotlib来使用已安装的中文字体。可以通过以下代码示例来实现: ```python import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 指定字体文件路径 font_path = 'path/to/font/file.ttf' # 加载字体文件 font = FontProperties(fname=font_path) # 设置全局字体为中文字体 plt.rcParams['font.family'] = font.get_name() # 绘制图表的代码 # ... # 显示图表 plt.show() ``` 在上述代码中,您需要替换`font_path`为您安装的字体文件的路径。然后,通过`FontProperties`类加载字体文件,并通过`plt.rcParams['font.family']`设置全局字体为中文字体。 3. 保存为图片(可选):如果您需要将图表保存为图片文件,并确保其中的中文码,可以使用如下代码: ```python # 保存图片 plt.savefig('path/to/save/image.png', dpi=300, bbox_inches='tight', transparent=True) ``` 在上述代码中,您需要替换`path/to/save/image.png`为您想要保存图片的路径。通过设置`dpi`参数可以调整图片的分辨率,使用`bbox_inches='tight'`可以自动裁剪空白边缘,使用`transparent=True`可以设置背景为透明。 通过以上步骤,您应该能够解决matplotlib中文问题。希望能对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值