绘制一个接一个的实时图

时间:2016-04-05 09:58:19

标签: python animation numpy matplotlib real-time

潜水员垂直向下跳入水中。

我想绘制一名潜水员在潜入水中之前的动作(从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()

1 个答案:

答案 0 :(得分:1)

现在代码正常运行。我测试了替换

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)

并且粒子以恒定速度移动。 所以看来你的潜水员在水实施中的位移有些不对劲。