美文网首页
Mac电脑python 使用Matplotlib画图中文字体显示

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

作者: 编程研究坊 | 来源:发表于2020-07-17 13:55 被阅读0次

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

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

#中文乱码处理

from matplotlib.font_manager import FontPropertiesdefgetChineseFont():returnFontProperties(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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/yajghktx.html