彻底解决matplotlib中文乱码问题
1.环境查看
a.系统版本查看
[hadoop@p168 ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
b.系统中文字体查看
[hadoop@p168 ~]$ fc-list :lang=zh
/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 Sharp:style=Regular
/usr/share/fonts/wqy-microhei/wqy-microhei.ttc: 文泉驿微米黑,文泉驛微米黑,WenQuanYi Micro Hei:style=Regular
/usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: 文泉驿等宽正黑,文泉驛等寬正黑,WenQuanYi Zen Hei Mono:style=Regular
/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: 文泉驿正黑,文泉驛正黑,WenQuanYi Zen Hei: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
c.Python版本及matplotlib配置文件位置查询
[hadoop@p168 ~]$ python
Python 2.7.10 (default, Dec 18 2015, 01:29:06)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> print matplotlib.matplotlib_fname()
/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-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(自己倾向于STZHONGS.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(STZhongsong),保存退出:
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.有字体但还是显示小方块,一般是没有删除~/.matplotlib/*.cache 的缓冲目录!
rm -rf ~/.matplotlib/*.cache
6.2017-08-04补充
目录类unix系统中,~/.fonts现在建议用~/.local/share/fonts替代了,所以也可将字体文件放在~/.local/share/fonts下,然后执行
- fc-cache -f -v ~/.local/share/fonts
更新字库缓存。这样会简单点,减低对python类库路径的依赖。
彻底解决matplotlib中文乱码问题的更多相关文章
- 彻底解决matplotlib中文乱码问题(转)
彻底解决matplotlib中文乱码问题 1.环境查看a.系统版本查看[hadoop@p168 ~]$ cat /etc/redhat-releaseCentOS Linux release 7.2. ...
- 解决matplotlib中文乱码问题(Windows)
1.修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.fam ...
- [python] virtualenv下解决matplotlib中文乱码
1. 安装中文字体 一般系统自带wqy-microhei,其ttc文件位于/usr/share/fonts/truetype/wqy/wqy-microhei.ttc 2. 将ttc文件复制到pyth ...
- Linux 系统下 matplotlib 中文乱码解决办法
亲测有效的方法之一: 1.下载中文字体simhei.ttf SimHei可以到http://fontzone.net/download/simhei下载 2.找到matplotlib相关的font文件 ...
- Matplotlib中文乱码解决办法
Matplotlib中文乱码 解决方法如下: 首先设置源码文件编码方式为UTF-8 #-*- coding: utf-8 -*- 接着设置字体属性字典 font = {'family': 'SimHe ...
- 解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/
解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/
- Ubuntu14.04安装中文输入法以及解决Gedit中文乱码问题
1 设置中文显示环境 1. 打开System Settings 2. 打开Personal-> Language Support. 会弹出如下对话框,提示你“语言支持没安装完整”. 点击“Rem ...
- Ubuntu14.04安装中文输入法以及解决Gedit中文乱码问题[转载]
转载自:http://www.cnblogs.com/zhcncn/p/4032321.html 写在前面:解决gedit 在txt文件格式出现乱码的问题,在我自己的操作中是需要把系统设置成中文显示环 ...
- 解决TortoiseCVS中文乱码
解决TortoiseCVS中文乱码必备,解决方法: 第一:卸载和TortoiseCVS安装一起安装的CVSNT. 第二:安装本版本CVSNT. CVSNT下载地址:http://down.51cto. ...
随机推荐
- 各种 Java Thread State 分析
转自:https://www.cnblogs.com/zhengyun_ustc/archive/2013/03/18/tda.html 1,线程状态为“waiting for monitor ent ...
- 动态规划算法(Dynamic Programming,简称 DP)
动态规划算法(Dynamic Programming,简称 DP) 浅谈动态规划 动态规划算法(Dynamic Programming,简称 DP)似乎是一种很高深莫测的算法,你会在一些面试或算法书籍 ...
- Flink SQL项目实录
一.Flink SQL层级 为Flink最高层的API,易于使用,所以应用更加广泛,eg. ETL.统计分析.实时报表.实时风控等. Flink SQL所处的层级: 二.Flink聚合: 1.Wind ...
- RISC-V汇编指南
原文出处:https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md RISC-V Assembly Programmer's ...
- 【leetcode算法-简单】38. 报数
[题目描述] 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作 "one 1&qu ...
- Codeforces 1245 D. Shichikuji and Power Grid
传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...
- (五)sturts2+spring整合
一.Spring与Struts的整合 1.1:加入Spring的jar包.1.2:加入Struts的jar包.1.3:加入Struts与Spring的整合jar//struts2-spring-plu ...
- Android 把枪/PDA 扫描头自回车没用 处理方法
XML 控件加上属性 android:imeOptions="actionNone"
- c# 实体类转XML
/// <summary> /// 将实体类转换成XML /// </summary> /// <typeparam name="T">< ...
- Word文档转PDF方法探索
最近的项目中需要将Word转换为PDF文件,找了很多方法和组件,最后找到了一些方法,和大家分享. 一.使用微软官方自带转换方法 好处是写法方便,官方支持,缺点是需要在服务器上安装office,而且要配 ...