1.?繪制多個圖像單獨顯示(subplot)
import numpy as np
import matplotlib.pyplot as plt
#創建自變量數組
x= np.linspace(0,2*np.pi,500)
#創建函數值數組
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
#創建圖形
plt.figure(1)
#第一行第一列圖形
ax1 = plt.subplot(2,2,1)
#第一行第二列圖形
ax2 = plt.subplot(2,2,2)
#第二行
ax3 = plt.subplot(2,1,2)
#選擇ax1
plt.sca(ax1)
plt.plot(x,y1,'r-.')
plt.ylim(-1.5,1.5) #限定y axis
#選擇ax2
plt.sca(ax2)
plt.plot(x,y2,'g--')
plt.ylim(-1.5,1.5)
#選擇ax3
plt.sca(ax3)
plt.plot(x,y3,'b--')
plt.ylim(-1.5,1.5)
plt.savefig('.//result//3.6.png')
plt.show()
?2.多個函數繪制于一張圖
plt.legend() 表示圖例
plt.legend(loc=' best')
其中,loc的選擇有
?? ?center ? ? ? ? ? ?upper center ? ? ? best ? ? ? ? ? ? ? lower center ? ? lower right ? ? ? ?lower left ? ? ?
?? ?upper right ? ??right ? ?? ? ? ? ? ? ? ?upper left ? ? ??? center left ? ? ? ? center right?
在設置圖例之前,需要在plt.plot()中設置label
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,500)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.cos(x)-1
plt.plot(x,y1,color = 'r',label = 'sinx') #label每個plot指定一個字符串標簽
plt.plot(x,y2,'-.', color = 'b', label = 'cosx')
plt.plot(x,y3,'--', color = 'g', label = 'cosx-1')
plt.legend(loc=' best')
plt.savefig('.//result//3.7.png')
plt.show()
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
