柚子快報激活碼778899分享:Matplotlib
柚子快報激活碼778899分享:Matplotlib
Matplotlib
使用前需要先導(dǎo)入模塊
import matplotlib.pyplot as plt
import numpy as np
繪制x點和y點
plot()函數(shù)用于在圖表中繪制點(標(biāo)記) 默認情況下,會從點到點之間繪制一條直線 參數(shù)1是一個包含x軸上點的數(shù)組 參數(shù)2是一個包含y軸上點的數(shù)組
#從(0,0)到(6,250)畫一條直線
xpoints = np.array([0,6])
ypoints = np.array([0,250])
plt.plot(xpoints,ypoints)
plt.show()
2 無線繪圖
僅繪圖標(biāo)志,可以使用快捷字符串標(biāo)識符‘o’
xpoints = np.array([0,6])
ypoints = np.array([0,250])
plt.plot(xpoints,ypoints,'o')
plt.show()
繪制多點
繪制多點需要保證x軸和y軸上點的數(shù)量一致
xpoints = np.array([0,6,4,9])
ypoints = np.array([0,6,9,250])
plt.plot(xpoints,ypoints,'o')
plt.show()
默認x軸
如果不指定x軸的點,x軸的坐標(biāo)會默認從0,1,2開始計算,取決于y軸上點的數(shù)量
ypoints = np.array([0,6,9,16])
plt.plot(ypoints)
plt.show()
標(biāo)記
可以使用關(guān)鍵字參數(shù)marker,指定標(biāo)記強調(diào)的點 markersize或者ms設(shè)置標(biāo)記的大小
xpoints = np.array([0,6,9,16])
ypoints = np.array([0,6,9,16])
plt.plot(xpoints,ypoints,marker='o',ms=20)
plt.show()
格式化字符
fmt marker | line | color
#o標(biāo)記 :虛線 r紅色
plt.plot(xpoints,ypoints,'o:r')
標(biāo)記參考
標(biāo)記描述‘o’Circle‘*’Star‘.’Point‘,’Pixel‘x’X‘X’X (filled)‘+’Plus‘P’Plus (filled)‘s’Square‘D’Diamond‘d’Diamond (thin)‘p’Pentagon‘H’Hexagon‘h’Hexagon‘v’Triangle Down‘^’Triangle Up‘<’Triangle Left‘>’Triangle Right‘1’Tri Down‘2’Tri Up‘3’Tri Left‘4’Tri Right‘l’Vline‘_’Hline
線參考
語法描述‘-’實線‘:’虛線‘–’破折線‘-.’點劃線
顏色參考
語法描述‘r’Red‘g’Green‘b’Blue‘c’Cyan‘m’Magenta‘y’Yellow‘k’Black‘w’White
線型
關(guān)鍵字參數(shù):linestyle或ls更改線條樣式 linewidth 設(shè)置線的寬度
x1 = np.array([0,6,9,16])
y1 = np.array([0,6,9,16])
x2 = np.array([1,2,3,4])
y2 = np.array([0,6,9,8])
plt.plot(x1,y1,x2,y2)
plt.show()
xlabel()和ylabel()分別為x軸和y軸設(shè)置標(biāo)簽 title()為圖設(shè)置標(biāo)題 以上方法中的fontdict參數(shù)可以設(shè)置標(biāo)簽和標(biāo)題字體屬性 grid()顯示網(wǎng)格線 axis指定x軸或y軸 合法取值:both x y loc定位標(biāo)題 默認為center 合法取值:left right center
#設(shè)置字體
matplotlib.rcParams['font.sans-serif']=['KaiTi']
font1={'family':'serif','color':'blue','size':20}
font2={'family':'serif','color':'darkred','size':16}
x1 = np.array([0,6,9,16])
y1 = np.array([0,6,9,16])
plt.plot(x1,y1)
plt.title("測試",loc='left')
plt.xlabel("x軸",fontdict=font1)
plt.ylabel("y軸",fontdict=font2)
plt.show()
多圖
subplot()可以繪制多個圖 subplot根據(jù)三個參數(shù)設(shè)置布局 第一第二分別代表幾行幾列,第三代表當(dāng)前圖索引 超級標(biāo)題suptitle()
plt.subplot(2,2,1)
x1 = np.array([0,6,9,16])
y1 = np.array([0,6,9,16])
plt.plot(x1,y1)
plt.subplot(2,2,2)
x1 = np.array([0,6,9,16])
y1 = np.array([0,6,9,16])
plt.plot(x1,y1)
plt.suptitle("hello")
plt.show()
散點圖
scatter() 該函數(shù)一樣需要傳入兩個相同長度的數(shù)組分別代表x和y軸
plt.subplot(2,2,1)
x1 = np.array([0,6,9,16])
y1 = np.array([0,6,9,16])
plt.scatter(x1,y1)
plt.show()
color或c屬性設(shè)置顏色 s屬性更改大小 alpha設(shè)置透明度 0-1 顏色圖 cmap指定顏色圖 colorbar顯示
#傳入數(shù)組可以定義不同顏色和大小
x = np.random.randint(100,size=100)
y = np.random.randint(100,size=100)
colors = np.random.randint(100,size=100)
sizes = np.random.randint(100,size=100)
plt.scatter(x,y,c=colors,alpha=0.6,s=sizes,cmap='viridis')
plt.colorbar()
plt.show()
柱狀圖
bar() barh()水平柱狀圖 屬性color width barh使用height設(shè)置高度
x=np.array(['a','b','c','d'])
y=np.random.randint(10,size=4)
plt.bar(x,y)
plt.barh(x,y)
plt.show()
直方圖
hist()
x=np.random.normal(60,10,260)
plt.hist(x)
plt.show()
餅圖
pie() label標(biāo)簽 傳入的數(shù)據(jù)只能是數(shù)組 startangle 起始角度 默認為0 explode 傳入的數(shù)據(jù)只能是數(shù)組 每個數(shù)值代表距離圓心的距離 shadow陰影 True 或 False legend()設(shè)置圖例 帶有參數(shù)title colors 傳入的數(shù)據(jù)只能是數(shù)組
y=np.array([10,26,14,60])
label=['apple','potato','clan','flty']
plt.pie(y,labels=label)
plt.legend()
plt.show()
柚子快報激活碼778899分享:Matplotlib
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。