1.環境查看
a.系統版本查看
[hadoop@p168 ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
b.系統中文字體查看python
[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=Lightlinux
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.第一種解決方法
在代碼中指定字體配置
- <span style="font-size:12px;">
- import matplotlib
- matplotlib.use('qt4agg')
- from matplotlib.font_manager import *
- import matplotlib.pyplot as plt
- myfont = FontProperties(fname='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc')
- matplotlib.rcParams['axes.unicode_minus']=False
- plt.plot([-1,2,-5,3])
- plt.title(u'中文',fontproperties=myfont)
- 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的緩衝目錄
第三在代碼中動態設置參數:
- <span style="font-size:12px;">
- import matplotlib
- matplotlib.use('qt4agg')
- matplotlib.rcParams['font.sans-serif'] = ['SimHei']
- matplotlib.rcParams['font.family']='sans-serif'
- matplotlib.rcParams['axes.unicode_minus'] = False
- plt.plot([-1,2,-5,3])
- plt.title(u'中文',fontproperties=myfont)
- plt.show()</span>
4.第三鍾解決辦法vim
首先將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,做用就是解決負號'-'顯示爲方塊的問題python2.7
5.常見問題oop
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]))spa
b.有字體但仍是顯示小方塊,通常是沒有刪除~/.cache/matplotlib 的緩衝目錄!.net
c.matplotlib.use('qt4agg')出錯,plt.show()沒顯示.
參考地址:http://blog.csdn.net/dgatiger/article/details/50414549