这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
画图中文显示问题 [2020/06/30 11:00] 10.1.46.173 |
画图中文显示问题 [2020/06/30 11:01] (当前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
====== 画图中文显示问题 ====== | ====== 画图中文显示问题 ====== | ||
+ | 创建日期 星期六 18 五月 2019 | ||
+ | |||
===== Windows ===== | ===== Windows ===== | ||
from pylab import * | from pylab import * | ||
行 17: | 行 19: | ||
# show() | # show() | ||
plt.savefig('s.png',dpi=1000) | plt.savefig('s.png',dpi=1000) | ||
- | ======== | + | ===== Linux ===== |
- | 创建日期 星期六 18 五月 2019 | + | |
+ | |||
+ | |||
- | 1.环境查看 | + | 1.环境查看 |
- | ------ | + | ------ |
- | ### a.系统版本查看 | + | ### a.系统版本查看 |
- | [hadoop@p168 ~]$ cat /etc/redhat-release | + | [hadoop@p168 ~]$ cat /etc/redhat-release |
- | CentOS Linux release 7.2.1511 (Core) | + | CentOS Linux release 7.2.1511 (Core) |
- | ### b.系统中文字体查看 | + | ### b.系统中文字体查看 |
- | ``` | + | ``` |
- | [hadoop@p168 ~]$ fc-list :lang=zh | + | [hadoop@p168 ~]$ fc-list :lang=zh |
- | /usr/share/fonts/wqy-microhei/wqy-microhei.ttc: 文泉驿等宽微米黑,文泉驛等寬微米黑,WenQuanYi Micro Hei Mono:style=Regular | + | /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-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/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/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 Mono:style=Regular |
- | /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc: 文泉驿正黑,文泉驛正黑,WenQuanYi Zen Hei: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 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 HK:style=Light |
- | /usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing CN:style=Light | + | /usr/share/fonts/cjkuni-uming/uming.ttc: AR PL UMing CN:style=Light |
- | ``` | + | ``` |
- | ### c.Python版本及matplotlib配置文件位置查询 | + | ### c.Python版本及matplotlib配置文件位置查询 |
- | ``` | + | ``` |
- | [hadoop@p168 ~]$ python | + | [hadoop@p168 ~]$ python |
- | Python 2.7.10 (default, Dec 18 2015, 01:29:06) | + | Python 2.7.10 (default, Dec 18 2015, 01:29:06) |
- | [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 | + | [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 |
- | Type "help", "copyright", "credits" or "license" for more information. | + | Type "help", "copyright", "credits" or "license" for more information. |
- | >>> import matplotlib | + | >>> import matplotlib |
- | >>> print matplotlib.matplotlib_fname() | + | >>> print matplotlib.matplotlib_fname() |
- | /home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc | + | /home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc |
- | >>> | + | >>> |
- | ``` | + | ``` |
- | 2.第一种解决方法 | + | 2.第一种解决方法 |
- | --------- | + | --------- |
- | 在代码中指定字体配置 | + | 在代码中指定字体配置 |
- | ``` | + | ``` |
- | #coding:utf-8 | + | #coding:utf-8 |
- | import matplotlib | + | import matplotlib |
- | matplotlib.use('qt4agg') | + | matplotlib.use('qt4agg') |
- | from matplotlib.font_manager import * | + | from matplotlib.font_manager import * |
- | import matplotlib.pyplot as plt | + | import matplotlib.pyplot as plt |
- | #定义自定义字体,文件名从1.b查看系统中文字体中来 | + | #定义自定义字体,文件名从1.b查看系统中文字体中来 |
- | myfont = FontProperties(fname='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc') | + | myfont = FontProperties(fname='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc') |
- | #解决负号'-'显示为方块的问题 | + | #解决负号'-'显示为方块的问题 |
- | matplotlib.rcParams['axes.unicode_minus']=False | + | matplotlib.rcParams['axes.unicode_minus']=False |
- | plt.plot([-1,2,-5,3]) | + | plt.plot([-1,2,-5,3]) |
- | plt.title(u'中文',fontproperties=myfont) | + | plt.title(u'中文',fontproperties=myfont) |
- | plt.show() | + | plt.show() |
- | ``` | + | ``` |
- | 3.第二种解决办法 | + | 3.第二种解决办法 |
- | --------- | + | --------- |
- | 首先将windwos中fonts目录下的simhei.ttf拷贝到/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf(文件路径参考1.c,根据实际情况修改)目录中, | + | 首先将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的缓冲目录 |
- | 第三在代码中动态设置参数: | + | 第三在代码中动态设置参数: |
- | ``` | + | ``` |
- | #coding:utf-8 | + | #coding:utf-8 |
- | import matplotlib | + | import matplotlib |
- | matplotlib.use('qt4agg') | + | matplotlib.use('qt4agg') |
- | #指定默认字体 | + | #指定默认字体 |
- | matplotlib.rcParams['font.sans-serif'] = ['SimHei'] | + | matplotlib.rcParams['font.sans-serif'] = ['SimHei'] |
- | matplotlib.rcParams['font.family']='sans-serif' | + | matplotlib.rcParams['font.family']='sans-serif' |
- | #解决负号'-'显示为方块的问题 | + | #解决负号'-'显示为方块的问题 |
- | matplotlib.rcParams['axes.unicode_minus'] = False | + | matplotlib.rcParams['axes.unicode_minus'] = False |
- | plt.plot([-1,2,-5,3]) | + | plt.plot([-1,2,-5,3]) |
- | plt.title(u'中文',fontproperties=myfont) | + | plt.title(u'中文',fontproperties=myfont) |
- | plt.show() | + | plt.show() |
- | ``` | + | ``` |
- | 4.第三钟解决办法 | + | 4.第三钟解决办法 |
- | --------- | + | --------- |
- | 首先将windwos中fonts目录下的simhei.ttf拷贝到/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf目录中, | + | 首先将windwos中fonts目录下的simhei.ttf拷贝到/home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf目录中, |
- | 然后删除~/.cache/matplotlib的缓冲目录 | + | 然后删除~/.cache/matplotlib的缓冲目录 |
- | 第三修改修改配置文件: | + | 第三修改修改配置文件: |
- | ``` | + | ``` |
- | [hadoop@p168 ~]$vim /home/hadoop/.pyenv/versions/2.7.10/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc | + | [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,保持退出。 | + | 文件路径参考1.c,根据实际情况修改,找到如下两项,去掉前面的#,并在font.sans-serif冒号后面加上SimHei,保持退出。 |
- | font.family : sans-serif | + | font.family : sans-serif |
- | font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif | + | font.sans-serif : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif |
- | 就是知道字库族为sans-serif,同时添加“SimHei”即宋体到字库族列表中,同时将找到 | + | 就是知道字库族为sans-serif,同时添加“SimHei”即宋体到字库族列表中,同时将找到 |
- | axes.unicode_minus,将True改为False,作用就是解决负号'-'显示为方块的问题 | + | axes.unicode_minus,将True改为False,作用就是解决负号'-'显示为方块的问题 |
- | 5.常见问题 | + | 5.常见问题 |
- | ------ | + | ------ |
- | ### a.当.../matplotlib/mpl-data/fonts/ttf中没有指定字体是执行时会出现如下错误. | + | ### 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 | + | 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])) | + | (prop.get_family(), self.defaultFamily[fontext])) |
- | ### b.有字体但还是显示小方块,一般是没有删除~/.matplotlib/*.cache 的缓冲目录! | + | ### b.有字体但还是显示小方块,一般是没有删除~/.matplotlib/*.cache 的缓冲目录! |
- | ``` | + | ``` |
- | rm -rf ~/.matplotlib/*.cache | + | rm -rf ~/.matplotlib/*.cache |
- | ``` | + | ``` |
- | ### c.matplotlib.use('qt4agg')出错,plt.show()没显示. | + | ### c.matplotlib.use('qt4agg')出错,plt.show()没显示. |
- | 原因:没有安装PyQt4,参见另外一篇博文《CentOS7.1下python2.7.10安装PyQt4》。 | + | 原因:没有安装PyQt4,参见另外一篇博文《CentOS7.1下python2.7.10安装PyQt4》。 |
- | ### d.补充 | + | ### d.补充 |
- | 目录类unix系统中,~/.fonts现在建议用~/.local/share/fonts替代了,所以也可将字体文件放在~/.local/share/fonts下,然后执行 | + | 目录类unix系统中,~/.fonts现在建议用~/.local/share/fonts替代了,所以也可将字体文件放在~/.local/share/fonts下,然后执行 |
- | ``` | + | ``` |
- | fc-cache -f -v ~/.local/share/fonts | + | fc-cache -f -v ~/.local/share/fonts |
- | ``` | + | ``` |
- | 更新字库缓存。这样会简单点,减低对python类库路径的依赖。 | + | 更新字库缓存。这样会简单点,减低对python类库路径的依赖。 |