1. 程式人生 > >python matplotlib 畫一條水平直線遇到的問題

python matplotlib 畫一條水平直線遇到的問題

"""niku 習題5.5"""
import numpy as np                 #使用import匯入模組numpy,並簡寫成np
import matplotlib.pyplot as plt    #使用import匯入模組matplotlib.pyplot,並簡寫成plt
plt.figure(figsize=(8,4))          #設定繪圖物件的寬度和高度

t = np.arange(0,1.1,0.1)
theta = 30+15*(t**2) 
theta_v = 30*t 
theta_accel = 30+t*0 

plt.plot(t,theta,label="$theta$",color="red",linewidth=2)
plt.plot(t,theta_v,label="$thetaV$",color="green",linewidth=2)  
plt.plot(t,theta_accel,label="$thetaAccel$",color="b")

t = np.arange(1,3.1,0.1)
theta = 45+30*(t-1) 
theta_v = 30+t*0  
theta_accel = 0 +t*0  

plt.plot(t,theta,label="$theta$",color="red",linewidth=2)
plt.plot(t,theta_v,label="$thetaV$",color="green",linewidth=2)  
plt.plot(t,theta_accel,label="$thetaAccel$",color="b",linewidth=2)

t = np.arange(3,4.1,0.1)
theta = 120-15*((4-t)**2)
theta_v = 30*(4-t) 
theta_accel = -30 +t*0  

plt.plot(t,theta,label="$theta$",color="red",linewidth=2)
plt.plot(t,theta_v,label="$thetaV$",color="green",linewidth=2)  
plt.plot(t,theta_accel,label="$thetaAccel$",color="b")

plt.ylim(-40,125)             #使用plt.ylim設定y座標軸範圍
plt.xlim(-1,5)
plt.xlabel("Time(s)")         #用plt.xlabel設定x座標軸名稱
'''設定圖例位置'''
#plt.legend(loc='upper right')  #設定圖例位置
plt.grid(True)
plt.show()