想要的圖像如下:

一開始是這樣畫的:
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
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="blue",linewidth=2)
t = np.arange(1,3.1,0.1)
theta = 45+30*(t-1)
theta_v = 30
theta_accel = 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="blue",linewidth=2)
t = np.arange(3,4.1,0.1)
theta = 120-15*((4-t)**2)
theta_v = 30*(4-t)
theta_accel = -30
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="blue")
plt.ylim(-40,200) #使用plt.ylim設置y坐標軸范圍
plt.xlim(-1,5)
plt.xlabel("Time(s)") #用plt.xlabel設置x坐標軸名稱
plt.legend(loc='upper left') #設置圖例位置
plt.grid(True)
plt.show()
#plt.plot(t,theta_accel,label="$thetaAccel$",color="blue",linewidth=2)
可以發現當theta_accel為常數時 plot失效,無法畫出圖像。
因為theta_accel = -30 不含變量t,改為:
theta_accel = -30 +t*0
則函數能夠畫出想要畫的圖像。
修改完,代碼如下:
"""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()
生成圖像如下:

成功!!!
