Mac电脑python 使用Matplotlib画图中文字体显示问题

问题:Mac电脑python 使用Matplotlib画图遇到中文会显示不出来如下图所示

解决办法:通过加入如下代码解决中文问题

 
#中文乱码处理
from matplotlib.font_manager import FontProperties
def getChineseFont():

    return FontProperties(fname='/System/Library/Fonts/PingFang.ttc')

遇到中文再前面加u字符以及plt函数里面加入fontproperties=getChineseFont()变量得以解决

示例如下:

 
#水平条形图#不同平台上数的价格#中文乱码处理
from matplotlib.font_manager import FontProperties
def getChineseFont():

    return FontProperties(fname='/System/Library/Fonts/PingFang.ttc')


import matplotlib.pyplot as plt

price=[39.5,40,45.4,38.9,33.34]
plt.barh(range(5),price,align='center',color='steelblue',alpha=0.8)
plt.xlabel(u'价格',fontproperties=getChineseFont())
plt.title(u'不同平台上的价格',fontproperties=getChineseFont())
plt.yticks(range(5),[u'亚马逊',u'当当网',u'中国图书网',u'京东',u'天猫'],fontproperties=getChineseFont())
plt.xlim([32,47])

for x,y in enumerate(price):
    plt.text(y+0.1,x,"%s"%y,va='center')

plt.show()

运行结果:

更多精彩内容查看头条:https://www.toutiao.com/c/user/51389663891/#mid=1598871110677508

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程研究坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值