欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式

 更新時(shí)間:2023年05月30日 15:12:37   作者:htfenght  
這篇文章主要介紹了matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評論