在Python Funcanimation中进行褪色的踪迹
我有以下代码,该代码使用第四阶runge kutta来求解双子摆的运动方程。我试图使用Funcanimation来动画两个摆的位置。我尝试删除每个坐标列表的0个元素,然后每10个新元素被绘制。我认为这会产生褪色的跟踪效果。相反,它只是绘制了一分。 (注意X1和Y1是第一个摆的坐标,而X2和Y2是第二个耦合摆的坐标)我觉得自己已经使事情变得复杂了。当我删除时,如果我%10 == 1:
循环循环代码良好,但不会删除任何以前的点。我觉得这应该很容易做到,但是我找不到任何直截了当的示例,并且文档并不非常有用。
简而言之,我有4个坐标列表来描述两个对象的位置。我想对这两个对象的当前位置进行动画,而不是任何以前的位置。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
from itertools import count
from IPython import display
h=.001
t=0
l1=1
l2=1
m1=1
m2=1
theta1= 0
theta2 = np.pi/2
thetadot1=0
thetadot2=0
g=9.8
x1=[]
y1=[]
x2=[]
y2=[]
E=[]
T=[]
Theta_dot1=[]
Theta_dot2=[]
kinetic_energy=[]
potential_energy=[]
while(t<4):
KE = .5*m1*((thetadot1*l1*np.cos(theta1))**2+(thetadot1*l1*np.sin(theta1))**2) + .5*m2*((thetadot1*l1*np.cos(theta1)+thetadot2*l2*np.cos(theta2))**2+(thetadot1*l1*np.sin(theta1)+thetadot2*l2*np.sin(theta2))**2)
PE = (m1*g*(l1-l1*np.cos(theta1))+m2*g*(l1+l2-l1*np.cos(theta1)-l2*np.cos(theta2)))
E.append(KE+PE)
kinetic_energy.append(KE)
potential_energy.append(PE)
Theta_dot1.append(thetadot1)
Theta_dot2.append(thetadot2)
T.append(t)
x1.append(l1*np.sin(theta1))
y1.append(-l1*np.cos(theta1))
x2.append(l1*np.sin(theta1)+l2*np.sin(theta2))
y2.append(-l1*np.cos(theta1)-l2*np.cos(theta2))
P1 = (-g*(2*m1+m2)*np.sin(theta1)-m2*g*np.sin(theta1-2*theta2)-2*np.sin(theta1-theta2)*m2*(l2*thetadot2**2+l1*thetadot1**2*(np.cos(theta1-theta2))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1-theta2)))))
L1 = (2*np.sin(theta1-theta2)*(l1*thetadot1**2*(m1+m2)+np.cos(theta1)*g*(m1+m2)+l2*m2*thetadot2**2*np.cos(theta1-theta2)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1-theta2)))))
G1 = thetadot1
H1 = thetadot2
thetadot1_1 = thetadot1 + (h/2)*P1
thetadot2_1 = thetadot2 + (h/2)*L1
theta1_1 = theta1 + (h/2)*G1
theta2_1 = theta2 + (h/2)*H1
P2 = (-g*(2*m1+m2)*np.sin(theta1_1)-m2*g*np.sin(theta1_1-2*theta2_1)-2*np.sin(theta1_1-theta2_1)*m2*(l2*thetadot2_1**2+l1*thetadot1_1**2*(np.cos(theta1_1-theta2_1))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_1-theta2_1)))))
L2 = (2*np.sin(theta1_1-theta2_1)*(l1*thetadot1_1**2*(m1+m2)+np.cos(theta1_1)*g*(m1+m2)+l2*m2*thetadot2_1**2*np.cos(theta1_1-theta2_1)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_1-theta2_1)))))
G2 = thetadot1_1
H2 = thetadot2_1
thetadot1_2 = thetadot1_1 + (h/2)*P2
thetadot2_2 = thetadot2_1 + (h/2)*L2
theta1_2 = theta1_1 + (h/2)*G2
theta2_2 = theta2_1 + (h/2)*H2
P3 = (-g*(2*m1+m2)*np.sin(theta1_2)-m2*g*np.sin(theta1_2-2*theta2_2)-2*np.sin(theta1_2-theta2_2)*m2*(l2*thetadot2_2**2+l1*thetadot1_2**2*(np.cos(theta1_2-theta2_2))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_2-theta2_2)))))
L3 = (2*np.sin(theta1_2-theta2_2)*(l1*thetadot1_2**2*(m1+m2)+np.cos(theta1_2)*g*(m1+m2)+l2*m2*thetadot2_2**2*np.cos(theta1_2-theta2_2)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_2-theta2_2)))))
G3 = thetadot1_2
H3 = thetadot2_2
thetadot1_3 = thetadot1_2 + (h/2)*P3
thetadot2_3 = thetadot2_2 + (h/2)*L3
theta1_3 = theta1_2 + (h/2)*G3
theta2_3 = theta2_2 + (h/2)*H3
P4 = (-g*(2*m1+m2)*np.sin(theta1_3)-m2*g*np.sin(theta1_3-2*theta2_3)-2*np.sin(theta1_3-theta2_3)*m2*(l2*thetadot2_3**2+l1*thetadot1_3**2*(np.cos(theta1_3-theta2_3))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_3-theta2_3)))))
L4 = (2*np.sin(theta1_3-theta2_3)*(l1*thetadot1_3**2*(m1+m2)+np.cos(theta1_3)*g*(m1+m2)+l2*m2*thetadot2_3**2*np.cos(theta1_3-theta2_3)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_3-theta2_3)))))
G4 = thetadot1_3
H4 = thetadot2_3
thetadot1 = thetadot1 + (h/6.0) * (P1+(2.*P2)+(2.0*P3) + P4)
thetadot2 = thetadot2 + (h/6.0) * (L1+(2.*L2)+(2.0*L3) + L4)
theta1 = theta1 + (h/6.0) * (G1+(2.*G2)+(2.0*G3) + G4)
theta2 = theta2 + (h/6.0) * (H1+(2.*H2)+(2.0*H3) + H4)
t=t+h
fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (15,5))
axes.set_ylim(-2.5, 2.5)
axes.set_xlim(-2.5, 2.5)
plt.style.use("ggplot")
x_1,y_1,x_2,y_2 = [], [], [], []
def animate(i):
if i % 10 ==1: #this is the stuff that is not working
x_1.remove(0)
y_1.remove(0)
x_2.remove(0)
y_2.remove(0)
x_1.append(x1[i*10])
y_1.append(y1[i*10])
x_2.append(x2[i*10])
y_2.append(y2[i*10])
axes.plot(x_1,y_1,'.', color="red")
axes.plot(x_2,y_2,'.', color="gray", linewidth=0.5)
anim = FuncAnimation(fig, animate, interval=.1)
I have the following code that uses 4th order Runge Kutta to solve the equations of motion of a double pendulum. I am attempting to animate the positions of the two pendulums using FuncAnimation. I tried removing the 0th element of each coordinate list after every 10th new element is plotted. I thought this would create a fading trail effect. Instead it just plotted one point. (note x1 and y1 are the coordinates of the first pendulum, and x2 and y2 are the coordinates of the second coupled pendulum) I feel like I'm over complicating things. when I remove the if i % 10 ==1:
loop the code animates fine, but it does not delete any previous points. I feel this should be fairly easy to do but I could not find any straight forward examples, and the documentation was not extremely helpful.
In short I have 4 lists of coordinates that describe the position of two objects. I want to animate the current position of these two objects and not any previous positions.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
from itertools import count
from IPython import display
h=.001
t=0
l1=1
l2=1
m1=1
m2=1
theta1= 0
theta2 = np.pi/2
thetadot1=0
thetadot2=0
g=9.8
x1=[]
y1=[]
x2=[]
y2=[]
E=[]
T=[]
Theta_dot1=[]
Theta_dot2=[]
kinetic_energy=[]
potential_energy=[]
while(t<4):
KE = .5*m1*((thetadot1*l1*np.cos(theta1))**2+(thetadot1*l1*np.sin(theta1))**2) + .5*m2*((thetadot1*l1*np.cos(theta1)+thetadot2*l2*np.cos(theta2))**2+(thetadot1*l1*np.sin(theta1)+thetadot2*l2*np.sin(theta2))**2)
PE = (m1*g*(l1-l1*np.cos(theta1))+m2*g*(l1+l2-l1*np.cos(theta1)-l2*np.cos(theta2)))
E.append(KE+PE)
kinetic_energy.append(KE)
potential_energy.append(PE)
Theta_dot1.append(thetadot1)
Theta_dot2.append(thetadot2)
T.append(t)
x1.append(l1*np.sin(theta1))
y1.append(-l1*np.cos(theta1))
x2.append(l1*np.sin(theta1)+l2*np.sin(theta2))
y2.append(-l1*np.cos(theta1)-l2*np.cos(theta2))
P1 = (-g*(2*m1+m2)*np.sin(theta1)-m2*g*np.sin(theta1-2*theta2)-2*np.sin(theta1-theta2)*m2*(l2*thetadot2**2+l1*thetadot1**2*(np.cos(theta1-theta2))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1-theta2)))))
L1 = (2*np.sin(theta1-theta2)*(l1*thetadot1**2*(m1+m2)+np.cos(theta1)*g*(m1+m2)+l2*m2*thetadot2**2*np.cos(theta1-theta2)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1-theta2)))))
G1 = thetadot1
H1 = thetadot2
thetadot1_1 = thetadot1 + (h/2)*P1
thetadot2_1 = thetadot2 + (h/2)*L1
theta1_1 = theta1 + (h/2)*G1
theta2_1 = theta2 + (h/2)*H1
P2 = (-g*(2*m1+m2)*np.sin(theta1_1)-m2*g*np.sin(theta1_1-2*theta2_1)-2*np.sin(theta1_1-theta2_1)*m2*(l2*thetadot2_1**2+l1*thetadot1_1**2*(np.cos(theta1_1-theta2_1))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_1-theta2_1)))))
L2 = (2*np.sin(theta1_1-theta2_1)*(l1*thetadot1_1**2*(m1+m2)+np.cos(theta1_1)*g*(m1+m2)+l2*m2*thetadot2_1**2*np.cos(theta1_1-theta2_1)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_1-theta2_1)))))
G2 = thetadot1_1
H2 = thetadot2_1
thetadot1_2 = thetadot1_1 + (h/2)*P2
thetadot2_2 = thetadot2_1 + (h/2)*L2
theta1_2 = theta1_1 + (h/2)*G2
theta2_2 = theta2_1 + (h/2)*H2
P3 = (-g*(2*m1+m2)*np.sin(theta1_2)-m2*g*np.sin(theta1_2-2*theta2_2)-2*np.sin(theta1_2-theta2_2)*m2*(l2*thetadot2_2**2+l1*thetadot1_2**2*(np.cos(theta1_2-theta2_2))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_2-theta2_2)))))
L3 = (2*np.sin(theta1_2-theta2_2)*(l1*thetadot1_2**2*(m1+m2)+np.cos(theta1_2)*g*(m1+m2)+l2*m2*thetadot2_2**2*np.cos(theta1_2-theta2_2)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_2-theta2_2)))))
G3 = thetadot1_2
H3 = thetadot2_2
thetadot1_3 = thetadot1_2 + (h/2)*P3
thetadot2_3 = thetadot2_2 + (h/2)*L3
theta1_3 = theta1_2 + (h/2)*G3
theta2_3 = theta2_2 + (h/2)*H3
P4 = (-g*(2*m1+m2)*np.sin(theta1_3)-m2*g*np.sin(theta1_3-2*theta2_3)-2*np.sin(theta1_3-theta2_3)*m2*(l2*thetadot2_3**2+l1*thetadot1_3**2*(np.cos(theta1_3-theta2_3))))/(l1*(2*m1+m2*(1-np.cos(2*(theta1_3-theta2_3)))))
L4 = (2*np.sin(theta1_3-theta2_3)*(l1*thetadot1_3**2*(m1+m2)+np.cos(theta1_3)*g*(m1+m2)+l2*m2*thetadot2_3**2*np.cos(theta1_3-theta2_3)) )/(l2*(2*m1+m2*(1-np.cos(2*(theta1_3-theta2_3)))))
G4 = thetadot1_3
H4 = thetadot2_3
thetadot1 = thetadot1 + (h/6.0) * (P1+(2.*P2)+(2.0*P3) + P4)
thetadot2 = thetadot2 + (h/6.0) * (L1+(2.*L2)+(2.0*L3) + L4)
theta1 = theta1 + (h/6.0) * (G1+(2.*G2)+(2.0*G3) + G4)
theta2 = theta2 + (h/6.0) * (H1+(2.*H2)+(2.0*H3) + H4)
t=t+h
fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (15,5))
axes.set_ylim(-2.5, 2.5)
axes.set_xlim(-2.5, 2.5)
plt.style.use("ggplot")
x_1,y_1,x_2,y_2 = [], [], [], []
def animate(i):
if i % 10 ==1: #this is the stuff that is not working
x_1.remove(0)
y_1.remove(0)
x_2.remove(0)
y_2.remove(0)
x_1.append(x1[i*10])
y_1.append(y1[i*10])
x_2.append(x2[i*10])
y_2.append(y2[i*10])
axes.plot(x_1,y_1,'.', color="red")
axes.plot(x_2,y_2,'.', color="gray", linewidth=0.5)
anim = FuncAnimation(fig, animate, interval=.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要褪色线,则必须处理
linecollection
的额外复杂性,这很难主化。If you want fading lines you have to deal with the extra complexity of
LineCollection
, which is not easy to master.