徹底解決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()沒顯示.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章