matplotlib(3)

plt.gcf()與plt.gca()

當前的圖表和子圖可以使用plt.gcf()和plt.gca()獲得。可讓邊框變換顏色。

x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,'r-*')
ax = plt.gca()
ax.spines["right"].set_color('red')#右邊
ax.spines["left"].set_color('green')#左邊
ax.spines["top"].set_color('yellow')#上
ax.spines["bottom"].set_color('orange')#下

在這裏插入圖片描述

若把參數改爲set_color(‘none’),則邊框消失
plt.figure("sei",figsize = (5,5),dpi = 100)#設置畫布大小
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,'r-*')
ax = plt.gca()
ax.spines["right"].set_color('none')
ax.spines["left"].set_color('g')
ax.spines["top"].set_color('none')
ax.spines["bottom"].set_color('y')
#座標軸先讓右和上的邊框消失
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

在這裏插入圖片描述

plt.figure()

figure(num=None,figsize=None,dpi=None,facecolor=None,edgecolor =None,frameon=True)

  • num:圖像編號或名稱,數字爲編號,字符串爲名稱
  • figsize:指定figure的寬和高,單位爲英寸
  • dpi:參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值爲80
  • facecolor:背景顏色
  • edgecolor:邊框顏色
  • frameon:是否顯示邊框

plt.savefig()

可以將畫出的圖保存下來
plt.savefig("F:\\xixi.jpg")#可自定義名字

條形圖

a = np.arange(10)
data = np.random.randint(1,11,10)
data
array([ 3,  1,  3,  9,  9,  8,  1,  9,  4, 10])
plt.bar(a,data,facecolor = 'orange',edgecolor = 'red',lw = 3,hatch = '.',width = 0.7,alpha = 0.6)

在這裏插入圖片描述

plt.barh(a,data,alpha = 0.6)

在這裏插入圖片描述

index = np.arange(5)
data1 = np.array([3,4,6,8,9])
data2 = np.array([11,23,6,5,1])
data3 =np.array ([12,6,21,8,26])
plt.bar(index,data1,color = 'yellow',label = 'a')
plt.bar(index,data2,bottom = data1,color = 'orange',label = 'b')
plt.bar(index,data3,bottom = (data2 + data1),color = 'red',label = 'c')

在這裏插入圖片描述

index = np.arange(5)
data1 = [3,4,6,8,9]
data2 = [11,23,6,5,1]
data3 =[12,6,21,8,26]
b = 0.3
plt.bar(index,data1,b,color = 'yellow',label = 'a')
plt.bar(index+b,data2,b,color = 'orange',label = 'b')
plt.bar(index+b*2,data3,b,color = 'red',label = 'c')

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章