python 單獨設置在plot每條線的label為中文


部分代碼如下:

fig,(scatter,plot) = plt.subplots(1,2,figsize=(16,8))
scatter.scatter(range(len(data)),np.true_divide(data[::-1,2],data[::-1,3]))
scatter.plot([1]*len(data),label='1/1')
scatter.set_xticks(range(len(data)))
scatter.set_xticklabels(data[::-1,0],fontproperties='Kaiti',fontsize='13',rotation=-90)
scatter.legend(loc='best')
scatter.set_ylabel('男/女',fontproperties='Kaiti',fontsize='20')
scatter.set_xlabel('年份',fontproperties='Kaiti',fontsize='20')
scatter.set_title('男女比例隨時間變化圖',fontproperties='Kaiti',fontsize='25')

plot.plot(data[::-1,4],color="R",label='城鎮')
plot.plot(data[::-1,5],color="B",label='農村')
plot.set_xticks(range(len(data)))
plot.set_xticklabels(data[::-1,0],fontproperties='Kaiti',fontsize='13',rotation=-90)
plot.set_ylabel('人口(萬人)',fontproperties='Kaiti',fontsize='20')
plot.set_xlabel('年份',fontproperties='Kaiti',fontsize='20')
plot.set_title('城鎮、農村人口隨時間變化圖',fontproperties='Kaiti',fontsize='25')
plt.legend(loc='best',prop={'family':'SimHei','size':14})
plt.show()

 

 

在一般的如刻度上,我們可以設置其屬性fontproperties用以局部設置顯示中文,但是對於plot的label屬性這樣的設置會有問題,解決方法如下:

plt.legend(prop={'family':'SimHei','size':14})

在放置圖例的時候進行prop設置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM