python 1: 解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题...

问题:

我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢?

 

解决:

1.指定默认编码为UTF-8:

在python代码开头加入如下代码

import  sys  
reload(sys)  
sys.setdefaultencoding('utf-8')  

2.确认你ubuntu系统环境下拥有的中文字体文件:

在终端运行命令"fc-list :lang=zh",得到自己系统的中文字体

命令输出如下:

/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai CN:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai HK:style=Book
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW:style=Book
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei,文泉驛微米黑,文泉驿微米黑:style=Regular
/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular
/usr/share/fonts/truetype/arphic/ukai.ttc: AR PL UKai TW MBE:style=Book
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW:style=Light
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing CN:style=Light
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing HK:style=Light
/usr/share/fonts/truetype/wqy/wqy-microhei.ttc: WenQuanYi Micro Hei Mono,文泉驛等寬微米黑,文泉驿等宽微米黑:style=Regular

我从中选择了Droid Sans Fallback字体。

3.在python代码中手动加载中文字体:

示例代码如下:

 1 #coding:utf-8
 2 from matplotlib.font_manager import FontProperties
 3 import matplotlib.pyplot as plt
 4 font = FontProperties(fname='/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf', size=14)
 5 plt.figure()
 6 plt.plot([1, 2, 3])
 7 plt.xlabel(u"电压差(V)", fontproperties=font)
 8 plt.ylabel(u"介质损耗角差(度)", fontproperties=font)
 9 plt.title(u"介质损耗角和荷电状态SOC关系图",fontproperties=font)
10 fig_name = '训练性能' + '.pdf'
11 plt.savefig(fig_name)
12 plt.show()

 

参考资料:

ubuntu下matplotlib中文字体的设置

Linux下python matplotlib.pyplot在图像上显示中文的问题

 

转载于:https://www.cnblogs.com/dmzhuo/p/6663468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值