Matplotlib汇图中文乱码问题解决

本文复制源自: https://www.linuxidc.com/Linux/2019-03/157632.htm

 

当我们用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"

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

 

b.系统中文字体查看

linuxidc@linuxidc:~/www.linuxidc.com$ 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/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/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
 

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

Python 3.5.2 (default, Jul 10 2019, 11:58:48) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc'

 

2. 第一种解决方法: 在代码中指定字体配置        请注意, 这种方法只能用于plt.title, plt.xlabel, plt.ylabel, plt.bar里因为不带fontproperties变量, 所以不适用

#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()

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

 

3.第二种解决办法    自测这种方法会出错: font family ['serif'] not found, 但是无论如何这个Error都需要解决, 下边的方法会解决此问题

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

 

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

然后删除~/.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.第三种解决办法
a) 首先将Windwos中fonts目录下的SimHei.ttf拷贝到 /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/fonts/ttf  目录中,如果找不到, 从这里下载: https://github.com/StellarCN/scp_zh/raw/master/fonts/SimHei.ttf
b) 第三修改配置文件:    这一步可以解决第二种方法里提到的问题
~$ vi /usr/local/lib/python3.5/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”即宋体到字库族列表中
c) 删除~/.cache/matplotlib的缓冲目录    rm -rf ~/.cache/matplotlib/*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值