失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Ubuntu14.04 LTS(64bit)彻底解决matplotlib中文乱码问题

Ubuntu14.04 LTS(64bit)彻底解决matplotlib中文乱码问题

时间:2019-12-05 21:49:07

相关推荐

Ubuntu14.04 LTS(64bit)彻底解决matplotlib中文乱码问题

问题描述: matplotlib生成图型中汉字变为方框

转载:/dgatiger/article/details/50414549

亲测第二种解决方法,可用。一,三方法没试

1.环境查看

a.系统版本查看

liu@liu-virtual-machine:~/liu/resource/chapter3/demo/code$ cat /etc/os-release

NAME="Ubuntu"

VERSION="14.04.3 LTS, Trusty Tahr"

ID=ubuntu

ID_LIKE=debian

PRETTY_NAME="Ubuntu 14.04.3 LTS"

VERSION_ID="14.04"

HOME_URL="/"

SUPPORT_URL="/"

BUG_REPORT_URL="/ubuntu/"

b.系统中文字体查看

liu@liu-virtual-machine:~/liu/resource/chapter3/demo/code$ fc-list :lang=zh

/usr/share/fonts/X11/misc/18x18ja.pcf.gz: Fixed:style=ja

/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular

/usr/share/fonts/X11/misc/18x18ko.pcf.gz: Fixed:style=ko

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

>>>liu@liu-virtual-machine:~/liu/resource/chapter3/demo/code$ python

Python 2.7.15 |Anaconda, Inc.| (default, May 1 , 23:32:55)

[GCC 7.2.0] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import matplotlib

>>> print matplotlib.matplotlib_fname()

/home/liu/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc

2.解决办法

第一种解决方法

在代码中指定字体配置

#coding:utf-8import matplotlibmatplotlib.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']=Falseplt.plot([-1,2,-5,3])plt.title(u'中文',fontproperties=myfont)plt.show()

第二种解决办法

首先在Windows环境下将C:\windows\fonts目录下的simhei.ttf拷贝到/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/目录下,然后进入到/home/liu/.cache/matplotlib目录下,将fontList.json tex.cache文件都删除掉

最后使用rcParams参数指定字体为黑体

#coding:utf-8import matplotlib#指定默认字体matplotlib.rcParams['font.sans-serif'] = ['SimHei']matplotlib.rcParams['font.family']='sans-serif'#解决负号'-'显示为方块的问题matplotlib.rcParams['axes.unicode_minus'] = Falseplt.plot([-1,2,-5,3])plt.title(u'中文',fontproperties=myfont)plt.show()

第三钟解决办法

首先将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,作用就是解决负号'-'显示为方块的问题

3.常见问题

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.有字体但还是显示小方块,一般是没有删除~/.matplotlib/*.cache的缓冲目录!

rm -rf ~/.matplotlib/*.cache

c.matplotlib.use('qt4agg')出错,plt.show()没显示.

原因:没有安装PyQt4,参见另外一篇博文《CentOS7.1下python2.7.10安装PyQt4》。

4.-08-04补充

目录类unix系统中,~/.fonts现在建议用~/.local/share/fonts替代了,所以也可将字体文件放在~/.local/share/fonts下,然后执行

fc-cache -f -v ~/.local/share/fonts

更新字库缓存。这样会简单点,减低对python类库路径的依赖。

如果觉得《Ubuntu14.04 LTS(64bit)彻底解决matplotlib中文乱码问题》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。