繪製圖形要求:
在x的取值範圍[0, 2]之間繪製函數:
繪製5副圖
'''
plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1)
說明:設定網格,選中網格,確定選中行列區域數量,編號從0開始
plt.subplot2grid((3,3),(1,0),colspan=2)
plt.annotate(s, xy=arrow_crd, xytext=text_crd, arrowprops=dict)
'''
x=np.linspace(0,2,30)
y=np.power(np.sin(x-2),2)*np.power(np.e,-1*np.power(x,2))
plt.figure(figsize=(16,16))
ax1=plt.subplot2grid((3,3),(0,0),colspan=3)
ax1.plot(x,y)
ax1.set_title(r'函數示意圖$y=f(x)=sin^2(x-2)e^{-x^2}$')
ax1.set_xlabel("x")
ax1.set_ylabel("y")
ax1.axis([0,2,0,1]) #設置座標範圍 x y軸
ax1.grid(True) #設置網格線
ax2=plt.subplot2grid((3,3),(1,0),colspan=2)
ax2.plot(x,y,'r*')
ax2.set_title(r'函數示意圖$y=f(x)=sin^2(x-2)e^{-x^2}$')
ax2.set_xlabel("x")
ax2.set_ylabel("y")
ax2.axis([0,2,0,1]) #設置座標範圍 x y軸
ax2.grid(True) #設置網格線
ax3=plt.subplot2grid((3,3),(1,2),rowspan=2)
ax3.plot(x,y,'m-.o')
ax3.set_title(r'函數示意圖$y=f(x)=sin^2(x-2)e^{-x^2}$')
ax3.set_xlabel("x")
ax3.set_ylabel("y")
ax3.axis([0,2,0,1]) #設置座標範圍 x y軸
ax3.grid(True) #設置網格線
ax4=plt.subplot2grid((3,3),(2,0),rowspan=1)
ax4.plot(x,y,'g',label="注意legend出現")
ax4.set_title(r'函數示意圖$y=f(x)=sin^2(x-2)e^{-x^2}$')
ax4.set_xlabel("x")
ax4.set_ylabel("y")
ax4.legend(loc="upper right")#設置圖例位置
ax4.axis([0,2,0,1]) #設置座標範圍 x y軸
ax4.grid(True) #設置網格線
ax5=plt.subplot2grid((3,3),(2,1),rowspan=1)
ax5.plot(x,y)
ax5.set_title(r'函數示意圖$y=f(x)=sin^2(x-2)e^{-x^2}$')
ax5.set_xlabel("x")
ax5.set_ylabel("y")
ax5.annotate(r'注意箭頭',xy=(0.8,0.5),xytext=(1.1,0.7),
arrowprops=dict(facecolor='black',shrink=0.1,width=5))#設置箭頭
ax5.axis([0,2,0,1]) #設置座標範圍 x y軸
ax5.grid(True) #設置網格線
plt.show()
解決負號無法顯示:
plt.rcParams['axes.unicode_minus']=False # 正常顯示負號