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

#!/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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值