您的位置:首页 > 编程语言 > PHP开发

在matplotlib中,解决中文乱码问题

2018-03-05 15:33 633 查看
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
font = FontProperties(fname='C:\\Windows\\Fonts\\simsun.ttc', size=14)
#将(0,10)分成等区间的100份
x=np.linspace(0,10,100)
#在matplotlib中使用数学函数的方法
y=np.sin(x)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(x,y)
ax.set_title('正弦函数图',fontproperties=font)
plt.show()
这里采用的是matplotlib中的font_manager方法:
(1)先导入font_manager
from matplotlib.font_manager import FontProperties
(2)找到中文字体在电脑中的位置
font = FontProperties(fname='C:\\Windows\\Fonts\\simsun.ttc', size=14)
(3)解决问题:
ax.set_title('正弦函数图',fontproperties=font)
效果如图所示:



拓展:如果图例中出现了乱码就这么修改: plt.legend(prop=font)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: