如何绘制实时图表,两个轴都取决于时间?

How to plot real-time graph, with both axis dependent on time?

我想制作一个显示潜水员跳入水中的动画。

根据潜水员离水的原始高度 h 和他的质量 m 的给定参数,我在 Python 中定义了一个程序来计算他触及水面的时刻水,Tc .

知道他垂直跳跃,X轴固定,并且 Y轴服从方程(1/2)gt^2 + h(g为万有引力常数)

如何在时间 t 处于范围 (Tc) 时绘制图表,X 轴和 Y 轴显示潜水员的投影? (x是固定的,y取决于时间t

在图形中window我们应该看到一个从一定高度垂直向下'jumps'的点,没有看到投影line/trace。

这是我的部分作品。不知道在程序哪里引入Tc

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# 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=(-2, 2))
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.empty(n) ; x.fill(1)   # the vertical position is fixed on x-axis
    y = 0.5*g*i^2 + h             # the equation of diver's displacement on y axis

    line.set_data(x, y) 
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
        frames=200, interval=20, blit=True)

plt.show()

编辑:

这是整个程序。我应用并修改了@Mike Muller 给出的建议,但没有用。我不明白哪里出了问题。希望你能解开我的疑惑。

# -*- coding: utf-8 -*-
from math import *  
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

def Plongeon():
    h = input("height = ")
    h = float(h)

    m = input(" mass = ")
    m = float(m)
    global g
    g = 9.8
    g = float(g)

    global Tc        #calculate air time, Tc
    Tc = sqrt(2*h/g)
    Tc = round(Tc,2)
    print Tc



    # 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=(-2, h+1))  #ymax : initial height+1
    line, = ax.plot([], [], ' o', lw=2)


    Tc = int(Tc+1)       #make Tc an integer to be used later in def get_y()
    xs = [1] # the vertical position is fixed on x-axis
    ys = [h, h]

    # initialization function: plot the background of each frame
    def init():
        line.set_data([], [])
        return line,

    # animation function.  This is called sequentially
    def animate(y):
        ys[-1] = y
        line.set_data(xs, ys)
        return line,

    def get_y():
        for step in range(Tc):
            t = step / 100.0
            y = -0.5*g*t**2 + h  # the equation of diver's displacement on y axis
        yield y

    # 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()
Plongeon()

根据原问题回答

您需要使用生成器来生成您的 y 数据。这有效:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# 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=(-2, 2))
line, = ax.plot([], [], ' o', lw=2)
g = 9.81
h = 2
tc = 200
xs = [1] # the vertical position is fixed on x-axis
ys = [h, h]


# animation function.  This is called sequentially
def animate(y):
    ys[-1] = y
    line.set_data(xs, ys)
    return line,

def get_y():
  for step in range(tc):
    t = step / 100.0
    y = -0.5*g*t**2 + h  # the equation of diver's displacement on y axis
    yield y

# 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()

综合回答

这应该有效:

# -*- coding: utf-8 -*-

from math import *
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation


def Plongeon():
    h = float(input("height = "))
    g = 9.81

    #calculate air time, Tc
    Tc = sqrt(2 * h / g)

    # 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=(-2, h+1))  #ymax : initial height+1
    line, = ax.plot([], [], ' o', lw=2)

    step = 0.01  # 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

    # 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()
Plongeon()

我删除了不需要的行。不需要 global。此外,质量从未在程序中的任何地方使用过。

这是最重要的部分:

def get_y():
     t = 0
     while t <= Tc:
         y = -0.5 * g * t**2 + h 
         yield y
         t += step

您需要将时间提前一个增量。