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.第一种解决方法
在代码中指定字体配置

  1. <span style="font-size:12px;">#coding:utf-8
  2. import matplotlib
  3. matplotlib.use('qt4agg')
  4. from matplotlib.font_manager import *
  5. import matplotlib.pyplot as plt
  6. #定义自定义字体,文件名从1.b查看系统中文字体中来
  7. myfont = FontProperties(fname='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc')
  8. #解决负号'-'显示为方块的问题
  9. matplotlib.rcParams['axes.unicode_minus']=False
  10. plt.plot([-1,2,-5,3])
  11. plt.title(u'中文',fontproperties=myfont)
  12. plt.show()</span>

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的缓冲目录
第三在代码中动态设置参数:

  1. <span style="font-size:12px;">#coding:utf-8
  2. import matplotlib
  3. matplotlib.use('qt4agg')
  4. #指定默认字体
  5. matplotlib.rcParams['font.sans-serif'] = ['SimHei']
  6. matplotlib.rcParams['font.family']='sans-serif'
  7. #解决负号'-'显示为方块的问题
  8. matplotlib.rcParams['axes.unicode_minus'] = False
  9. plt.plot([-1,2,-5,3])
  10. plt.title(u'中文',fontproperties=myfont)
  11. plt.show()</span>

 

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()没显示.
原因:没有安装PyQt4,参见另外一篇博文《CentOS7.1下python2.7.10安装PyQt4》。
 
 
 
参考地址:http://blog.csdn.net/dgatiger/article/details/50414549

彻底解决matplotlib中文乱码问题(转)的更多相关文章

  1. 彻底解决matplotlib中文乱码问题

    1.环境查看a.系统版本查看 [hadoop@p168 ~]$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) b.系统中文字 ...

  2. 解决matplotlib中文乱码问题(Windows)

    1.修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.fam ...

  3. [python] virtualenv下解决matplotlib中文乱码

    1. 安装中文字体 一般系统自带wqy-microhei,其ttc文件位于/usr/share/fonts/truetype/wqy/wqy-microhei.ttc 2. 将ttc文件复制到pyth ...

  4. Linux 系统下 matplotlib 中文乱码解决办法

    亲测有效的方法之一: 1.下载中文字体simhei.ttf SimHei可以到http://fontzone.net/download/simhei下载 2.找到matplotlib相关的font文件 ...

  5. Matplotlib中文乱码解决办法

    Matplotlib中文乱码 解决方法如下: 首先设置源码文件编码方式为UTF-8 #-*- coding: utf-8 -*- 接着设置字体属性字典 font = {'family': 'SimHe ...

  6. 解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/

    解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客  http://hsj69106.blog.51cto.com/1017401/595598/

  7. Ubuntu14.04安装中文输入法以及解决Gedit中文乱码问题

    1 设置中文显示环境 1. 打开System Settings 2. 打开Personal-> Language Support. 会弹出如下对话框,提示你“语言支持没安装完整”. 点击“Rem ...

  8. Ubuntu14.04安装中文输入法以及解决Gedit中文乱码问题[转载]

    转载自:http://www.cnblogs.com/zhcncn/p/4032321.html 写在前面:解决gedit 在txt文件格式出现乱码的问题,在我自己的操作中是需要把系统设置成中文显示环 ...

  9. 解决TortoiseCVS中文乱码

    解决TortoiseCVS中文乱码必备,解决方法: 第一:卸载和TortoiseCVS安装一起安装的CVSNT. 第二:安装本版本CVSNT. CVSNT下载地址:http://down.51cto. ...

随机推荐

  1. Type Hierarchy

    Window - Preferences - General - Keys Name:         Open Type Hierarchy Description: Open a type hie ...

  2. mdb

    计划开发高性能KV数据库, 学习MongoDB leveldb innodb, 练手贴+日记贴: http://bbs.chinaunix.net/thread-4244870-1-1.html 超高 ...

  3. Python之安装第三方扩展库

    PyPI 地址:https://pypi.python.org/pypi 如果你知道你要找的库的名字,那么只需要在右上角搜索栏查找即可. 1.pip安装扩展库 (1)安装最新版本的扩展库: cmd&g ...

  4. socket API详解

    send函数 int send( SOCKET s,   const char FAR *buf,   int len,   int flags ); 不论是客户还是服务器应用程序都用send函数来向 ...

  5. xshell无法连接Ubuntu的解决办法

    使用workstations14安装完Ubunu后,网络连接方式为NAT模式(N):用于共享主机的IP地址 此时想用xshell连接此虚拟机但是提示连接失败,但是宿主机和虚拟机互相都能ping通,且虚 ...

  6. 学习做爬虫-vs2017

    最近新装了vs2017,安装过程发生了很大的变化,操作变的更加容易了. 下载vs安装程序进行安装.更新界面如图所示,我选择了安装免费个人版(这个是已安装的更新界面,和安装界面差不多) 如图所示,这样的 ...

  7. django LookUp

    Custom Lookups 一个简单LookUp例子 Author.objects.filter(name__ne='Jack') # Translate SQL "author" ...

  8. 201621123012 《Java程序设计》第11周学习总结

    作业11-多线程 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread ...

  9. css3导航鼠标经过移动、缩放、转动、拉长、拉伸

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. [AIR] 利用File获取应用程序根目录

    import flash.filesystem.File; var file1:File = new File(File.applicationDirectory.resolvePath(" ...