matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式
matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置
把字體都設(shè)置成為“Times New Roman”
plt.figure(figsize=[15,8]) plt.scatter(X, Y, label = 'RealValue') plt.plot(X, func(X, a, b), 'red', label = 'CurveLine') plt.title(station, fontdict={'family' : 'Times New Roman', 'size' : 16}) plt.ylabel('Clocks($\mu S$)', fontdict={'family' : 'Times New Roman', 'size' : 16}) plt.xlabel('Time', fontdict={'family' : 'Times New Roman', 'size' : 16}) plt.yticks(fontproperties = 'Times New Roman', size = 14) plt.xticks(fontproperties = 'Times New Roman', size = 14) plt.legend(prop={'family' : 'Times New Roman', 'size' : 16}) plt.savefig('./stationClocks/' + station + '.ps', dpi = 200) plt.show()
在matplotlib使用中文坐標(biāo)軸,設(shè)置坐標(biāo)軸,標(biāo)題字體及字體大小
1.matplotlib的坐標(biāo)軸中一般是不支持中文
在windows中要使用中文坐標(biāo)軸的話,需要調(diào)用電腦中的字體。
實(shí)現(xiàn)代碼:
from matplotlib import font_manager#導(dǎo)入字體管理模塊 my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF") #定義中文字體屬性,文字儲存路徑可以在C:/WINDOWS/Fonts/找到,這里我設(shè)置的宋體 plt.xlabel("時(shí)間",fontproperties = my_font,fontsize = 18) #在設(shè)置x坐標(biāo)中文標(biāo)注,令fontproperties = my_font,fontsize令字體為18號 #plt.title,plt.ylabel,plt.xticks,plt.yticks設(shè)置中文標(biāo)注類似
完整代碼實(shí)例如下:
from matplotlib import pyplot as plt#導(dǎo)入matplotlib from matplotlib import font_manager#導(dǎo)入字體管理模塊 import random#導(dǎo)入隨機(jī)生成模塊 my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF") #定義中文字體屬性,文字儲存路徑可以在C:/WINDOWS/Fonts/找到,這里我設(shè)置的宋體 y = [random.randint(20,35) for i in range(120)]#y值為120個(gè)在20-35之間隨機(jī)數(shù) x = range(0,120)#x值為0-120 plt.figure(figsize=(15,10),dpi=90)#圖片大小為15*10,每英寸90個(gè)像素點(diǎn) _x_labels = ["10點(diǎn){}分".format(i) for i in range(60)] _x_labels += ["11點(diǎn){}分".format(i) for i in range(60)]#設(shè)置x坐標(biāo)軸中文刻度 plt.xticks(list(x[::3]),_x_labels[::3],rotation=45,fontproperties=my_font,fontsize = 12)#坐標(biāo)軸刻度顯示步長為3,為了避免坐標(biāo)軸字體重疊,旋轉(zhuǎn)45度,fontproperties設(shè)置字體 plt.plot(x,y) plt.xlabel("時(shí)間",fontproperties = my_font,fontsize = 18)#設(shè)置x坐標(biāo)標(biāo)注,字體為18號 plt.ylabel("每分鐘對應(yīng)的溫度",fontproperties = my_font,fontsize = 18)#設(shè)置y坐標(biāo)標(biāo)注 plt.title("10點(diǎn)到12點(diǎn)每分鐘溫度變化圖",fontproperties = my_font,fontsize = 24)#設(shè)置標(biāo)題 plt.plot(x,y)#繪圖 plt.show()#顯示
圖片結(jié)果如圖所示:
2.繪制兩條折線及添加圖例,添加網(wǎng)格
from matplotlib import pyplot as plt from matplotlib import font_manager import random plt.figure(figsize=(10,8),dpi=80) my_font = font_manager.FontProperties(fname="C:/WINDOWS/Fonts/STSONG.TTF") x = range(11,31) y_1 = [random.randint(0,5) for i in range(20)] y_2 = [random.randint(0,5) for i in range(20)] _x_label = ["{}歲".format(i) for i in range(11,31)] plt.xticks(list(x[::2]),_x_label[::2],rotation = 45,fontproperties = my_font,fontsize = 12) plt.plot(x,y_1,label = "自己",color="red") plt.plot(x,y_2,label= "朋友",linestyle="--",linewidth=2,alpha = 0.4) plt.xlabel("年齡",fontproperties = my_font,fontsize = 18) plt.ylabel("每年交的朋友",fontproperties = my_font,fontsize = 18) plt.title("11歲到30歲每年交的朋友變化圖",fontproperties = my_font,fontsize = 24) plt.grid(alpha = 0.4)#添加網(wǎng)格,alpha = 0.4透明度 plt.legend(prop=my_font,loc='best')#添加圖例 plt.show()
圖片結(jié)果如圖所示:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié)
下面小編就為大家分享一篇python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Python使用plotly繪制數(shù)據(jù)圖表的方法
本篇文章主要介紹了Python使用plotly繪制數(shù)據(jù)圖表的方法,實(shí)例分析了plotly繪制的技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07使用Python實(shí)現(xiàn)Oracle數(shù)據(jù)庫自動巡檢程序
這篇文章主要為大家詳細(xì)介紹了如何創(chuàng)建一個(gè)Oracle數(shù)據(jù)庫自動巡檢程序,以確保數(shù)據(jù)庫的順暢運(yùn)行,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Pandas數(shù)據(jù)集的分塊讀取的實(shí)現(xiàn)
本文主要介紹了Pandas數(shù)據(jù)集的分塊讀取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息
這篇文章主要為大家詳細(xì)介紹了python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python修改pip install默認(rèn)安裝路徑的詳細(xì)步驟
pip安裝的第三方庫默認(rèn)存放在C盤中,為了便于管理和不過度占用C盤空間所以想修改默認(rèn)的pip路徑,文章通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04