一个接一个地绘制实时图表
Plot one real time graph after another
一名潜水员垂直跳入水中。
我想绘制潜水员在潜入水中之前的运动(从 t=0 到 t=Tc,Tc 在他接触水的那一刻),他的位置两个轴都取决于时间,然后是他的入水后的运动 (t > Tc)
我设法绘制了描述他入水前运动的实时图表,但如何在他入水后添加/替换为另一个方程式?
顺便问一下,如何跟踪水位,即 y=0 处的固定水平线?
这是我的代码:
# -*- coding: utf-8 -*-
from math import *
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
print ("Height of diver (m) and weight of diver (kg)")
h = float(input(" height = "))
m = float(input(" mass = "))
g = 9.81 #gravity = 10 m/s^2
Tc = sqrt(2*h/g) #Tc = the time the diver touches water
Tc = round(Tc,2)
Ve = g*Tc #Ve = His initial velocity before plunging into water
Ve = round (Ve,2)
## movement in the water
#calculation of velocity's limit
dh = 0.9 #bouyancy, dh=0.9
k = 250 #coefficient of friction , k =250 kg/s
rate = m/k
Vlim = rate*g*(1-(1/dh))
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-30, h+1)) #ymax : initial height+1
line, = ax.plot([], [], ' o', lw=2)
step = 0.1 # animation step
xs = [1] # the vertical position is fixed on x-axis
ys = [h]
# animation function. This is called sequentially
def animate(y):
ys[-1] = y
line.set_data(xs, ys)
return line,
def get_y():
t = 0
while t <= Tc:
y = -0.5 * g * t**2 + h # the equation of diver's displacement on y axis
yield y
t += step
while t > Tc:
y = rate*Ve*(exp(-Tc/rate)-exp(-t/rate)) + rate*(abs(Vlim))*(exp(-Tc/rate)-exp(-t/rate)) + (abs(Vlim))*(Tc-t) # equation of diver's displacement in water on y axis
yield y
t += step # Indentation Error fixed
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames=get_y, interval=100)
plt.show()
现在代码可以运行了。我测试了替换
y = -(t - Tc)
而不是
y = rate*Ve*(exp(-Tc/rate)-exp(-t/rate)) + rate*(abs(Vlim))*(exp(-Tc/rate)-exp(-t/rate)) + (abs(Vlim))*(Tc-t)
并且粒子以恒定速度运动。
所以看来你的潜水员在水中执行的位移有问题。
一名潜水员垂直跳入水中。
我想绘制潜水员在潜入水中之前的运动(从 t=0 到 t=Tc,Tc 在他接触水的那一刻),他的位置两个轴都取决于时间,然后是他的入水后的运动 (t > Tc)
我设法绘制了描述他入水前运动的实时图表,但如何在他入水后添加/替换为另一个方程式?
顺便问一下,如何跟踪水位,即 y=0 处的固定水平线?
这是我的代码:
# -*- coding: utf-8 -*-
from math import *
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
print ("Height of diver (m) and weight of diver (kg)")
h = float(input(" height = "))
m = float(input(" mass = "))
g = 9.81 #gravity = 10 m/s^2
Tc = sqrt(2*h/g) #Tc = the time the diver touches water
Tc = round(Tc,2)
Ve = g*Tc #Ve = His initial velocity before plunging into water
Ve = round (Ve,2)
## movement in the water
#calculation of velocity's limit
dh = 0.9 #bouyancy, dh=0.9
k = 250 #coefficient of friction , k =250 kg/s
rate = m/k
Vlim = rate*g*(1-(1/dh))
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-30, h+1)) #ymax : initial height+1
line, = ax.plot([], [], ' o', lw=2)
step = 0.1 # animation step
xs = [1] # the vertical position is fixed on x-axis
ys = [h]
# animation function. This is called sequentially
def animate(y):
ys[-1] = y
line.set_data(xs, ys)
return line,
def get_y():
t = 0
while t <= Tc:
y = -0.5 * g * t**2 + h # the equation of diver's displacement on y axis
yield y
t += step
while t > Tc:
y = rate*Ve*(exp(-Tc/rate)-exp(-t/rate)) + rate*(abs(Vlim))*(exp(-Tc/rate)-exp(-t/rate)) + (abs(Vlim))*(Tc-t) # equation of diver's displacement in water on y axis
yield y
t += step # Indentation Error fixed
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames=get_y, interval=100)
plt.show()
现在代码可以运行了。我测试了替换
y = -(t - Tc)
而不是
y = rate*Ve*(exp(-Tc/rate)-exp(-t/rate)) + rate*(abs(Vlim))*(exp(-Tc/rate)-exp(-t/rate)) + (abs(Vlim))*(Tc-t)
并且粒子以恒定速度运动。 所以看来你的潜水员在水中执行的位移有问题。