【问题标题】:Plot one real time graph after another绘制一张又一张的实时图
【发布时间】:2016-04-05 09:58:19
【问题描述】:

一名潜水员垂直跳入水中。

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

【问题讨论】:

  • 代码在t += step上有一个IndentationError
  • taux 未定义
  • 对不起,我把它换成了费率。
  • 在你的“水中”方程中有一个TypeError。可能缺少*rate*Ve*(exp(-Tc/rate)-exp(-t/rate)) + rate*(abs(Vlim))*(exp(-Tc/rate)-exp(-t/rate)) + (abs(Vlim))*(Tc-t)
  • 你是对的。我加了。

标签: python animation numpy matplotlib real-time


【解决方案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)

粒子以恒定速度运动。 所以看来你的潜水员在水中的位移执行有问题。

【讨论】:

  • 你是对的!顺便问一下如何在 y=0 处在背景中添加一条固定的水平线?
  • 这是题外话,无论如何你必须在你的代码中添加plt.axhline(0)。如果您解决了问题,请确认答案。
猜你喜欢
  • 2018-11-26
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多