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. [Lua快速了解一下]Lua的MetaTable和MetaMethod

    MetaTable和MetaMethod是Lua中的重要的语法,MetaTable主要是用来做一些类似于C++重载操作符式的功能. 两个分数 fraction_a = {numerator=, den ...

  2. ARM启动代码中_main 与用户主程序main()的区别

    1.1  问题描述     __main函数的作用是什么呀?1.2  问题剖析     __main函数是C/C++运行时库的一个函数,嵌入式系统在进入应用主程序之前必须有一个初始化的过程,使用__m ...

  3. 在Arch gnome中安装一些软件

    一. 在Arch gnome中添加ibus中文输入法 1. 安装ibus-libpinyin. sudo pacman -S ibus-libpinyin 如果系统之前没有安装ibus,则先通过pac ...

  4. 纯分享scp协议如何工作

    scp协议是什么, wiki上说: Secure copy or SCP is a means of securely transferring computer files between a lo ...

  5. [gcc warnings] -Wtrigraph warnings

    [gcc warnings] -Wtrigraph warnings 背景 三字符组(trigraph)与双字符组(Digraph)是程序设计语言(如C语言)中3个或者2个字符的序列,在编译器预扫描源 ...

  6. STM32开发(一):简介及开发环境

    1. 背景 STM32是意法(ST)公司开发的基于ARM Cortex-M系列的一系列微控制器(MCU). 有两种库 标准外设库(StdPeriph_Driver.Standard Periphera ...

  7. 线程概要 Java

    线程 进程和线程的区别 串行:初期的计算机只能串行执行任务,大量时间等待用户输入 批处理:预先将用户的指令集中成清单,批量串行处理用户指令,仍然无法并发执行 进程:进程独占内存空间,保存各自运行状态, ...

  8. Echart自定义y轴刻度信息1

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Squid代理服务器(二)——配置Squid服务器

    一.传统代理 (一)需求分析 局域网内,客户机访问自家的Web服务器,通过Squid代理服务器访问Web服务器,再由Squid反馈给客户机;在Squid主机上,构建Squid为客户机访问网站提供代理服 ...

  10. 使用 echart的jar包,传递到前台报 null错误

    表现:后台创建 Option的JsonObjject传递到前台之后,失败,F12控制台报错:无法定义null的属性xxx 原因:使用jar包里面的对象分装没有默认值,而我们自己使用时只是对需要的对象进 ...