彻底解决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.第一种解决方法
在代码中指定字体配置
  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()没显示.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于matplotlib中文乱码问题,可以按照以下步骤进行解决: 1. 安装字体库:首先,您可以下载并安装支持中文的字体库,比如SimHei或者Microsoft YaHei。可以从官方网站或者其他可信来源下载字体文件,并将其安装到系统中。 2. 配置matplotlib:接下来,您需要在代码中配置matplotlib来使用已安装的中文字体。可以通过以下代码示例来实现: ```python import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 指定字体文件路径 font_path = 'path/to/font/file.ttf' # 加载字体文件 font = FontProperties(fname=font_path) # 设置全局字体为中文字体 plt.rcParams['font.family'] = font.get_name() # 绘制图表的代码 # ... # 显示图表 plt.show() ``` 在上述代码中,您需要替换`font_path`为您安装的字体文件的路径。然后,通过`FontProperties`类加载字体文件,并通过`plt.rcParams['font.family']`设置全局字体为中文字体。 3. 保存为图片(可选):如果您需要将图表保存为图片文件,并确保其中的中文不乱码,可以使用如下代码: ```python # 保存图片 plt.savefig('path/to/save/image.png', dpi=300, bbox_inches='tight', transparent=True) ``` 在上述代码中,您需要替换`path/to/save/image.png`为您想要保存图片的路径。通过设置`dpi`参数可以调整图片的分辨率,使用`bbox_inches='tight'`可以自动裁剪空白边缘,使用`transparent=True`可以设置背景为透明。 通过以上步骤,您应该能够解决matplotlib中文乱码问题。希望能对您有所帮助!如果您还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值