1.環境查看
a.系統版本查看
[hadoop@p168 ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
/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
[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.第一種解決方法
在代碼中指定字體配置
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的緩沖目錄
第三在代碼中動態設置參數:
然后刪除~/.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”即宋體到字庫族列表中,同時將找到
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.第一種解決方法
在代碼中指定字體配置
- <span style="font-size:12px;">#coding:utf-8
- import matplotlib
- matplotlib.use('qt4agg')
- from matplotlib.font_manager import *
- import matplotlib.pyplot as plt
- #定義自定義字體,文件名從1.b查看系統中文字體中來
- 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;">#coding:utf-8
- 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.第三鍾解決辦法
首先將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