Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 548|回复: 0

彻底解决matplotlib中文乱码问题

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-9-5 13:48:58 | 显示全部楼层 |阅读模式

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

    [python] view plain copy
    1. #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()  


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

    [python] view plain copy
    1. #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()  

     

    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
    c.matplotlib.use('qt4agg')出错,plt.show()没显示.
    原因:没有安装PyQt4,参见另外一篇博文《CentOS7.1下python2.7.10安装PyQt4》。

    6.2017-08-04补充

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

    [plain] view plain copy
    1. fc-cache -f -v ~/.local/share/fonts  

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

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-29 23:00 , Processed in 0.073528 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表