#範例2-3:使用numpy+matplotlib, 繪圖sin(x),cos(x) import numpy as np import matplotlib.pyplot as plt #一個圖:sin(x) #numpy的pi = np.pi x = np.arange(0,np.pi*8,0.01) #numpy 的sin = np.sin y = np.sin(x) plt.plot(x,y,color='red') plt.show() #二個圖:sin(x), cos(x) x = np.arange(-np.pi*4,np.pi*4,0.01) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x,y1,color='red',label='y=sinx') plt.plot(x,y2,color='blue',label='y=cosx') plt.title = 'sin(x) and cos(x)' plt.xlabel('angel(pi)') plt.ylabel('amplitude',rotation=0) #圖例標籤 plt.legend() #plt.legend(['y=sinx','y=cosx']) plt.show() #三個圖:sin(x), cos(x), sin(x)+cos(x) x = np.arange(-np.pi*4,np.pi*4,0.01) y1 = np.sin(x) y2 = np.cos(x) y3 = y1 + y2 plt.plot(x,y1,color='red',label='y=sinx') plt.plot(x,y2,color='blue',label='y=cosx') plt.plot(x,y3,color='green',label='y=sinx+cosx') plt.title = 'sin(x) and cos(x)' #設定xlabe,ylabell的位置:position= plt.xlabel('angel(pi)',position=(0.95,0)) plt.ylabel('amplitude',position=(0,1),rotation=0) #圖例標籤的位置 loc = "lower left" ''' 'best' 0 'upper right' 1 'upper left' 2 'lower left' 3 'lower right' 4 'right' 5 'center left' 6 'center right' 7 'lower center' 8 'upper center' 9 'center' 10 ''' plt.legend(loc="lower left") #plt.legend(['y=sinx','y=cosx']) #設定:不顯示圖片的四邊黑框線 ax = plt.gca() ax.spines["top"].set_color("none") ax.spines["right"].set_color("none") ax.spines["bottom"].set_position(("data", 0)) ax.spines["left"].set_position(("data", 0)) plt.show()