Python 如何绘制实时图形,两个轴都依赖于时间?
我想制作一个显示潜水员跳入水中的动画 根据给定的潜水员离开水面的原始高度,Python 如何绘制实时图形,两个轴都依赖于时间?,python,animation,numpy,matplotlib,real-time,Python,Animation,Numpy,Matplotlib,Real Time,我想制作一个显示潜水员跳入水中的动画 根据给定的潜水员离开水面的原始高度,h和他的质量,m,我用Python定义了一个程序来计算他接触水面的时刻,Tc 知道他垂直跳跃,X轴是固定的,并且 Y轴服从方程式(1/2)gt^2+h(g为重力常数) 当时间t在范围内(Tc),X轴和Y轴显示潜水员的投影时,如何绘制图形?(x是固定的,y取决于时间t) 在图形窗口中,我们应该看到一个点从一定高度垂直向下“跳跃”,而看不到投影的线条/轨迹 这是我工作的一部分。我不知道在程序中从何处引入Tc: import n
h
和他的质量,m
,我用Python定义了一个程序来计算他接触水面的时刻,Tc
知道他垂直跳跃,X轴是固定的,并且
Y轴服从方程式(1/2)gt^2+h(g为重力常数)
当时间t
在范围内(Tc
),X轴和Y轴显示潜水员的投影时,如何绘制图形?(x
是固定的,y
取决于时间t
)
在图形窗口中,我们应该看到一个点从一定高度垂直向下“跳跃”,而看不到投影的线条/轨迹
这是我工作的一部分。我不知道在程序中从何处引入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()
你需要增加你的时间。非常感谢你,但是我的程序不起作用,我想可能有一部分我没有使用正确的符号。我在问题下方发布了我的完整程序,希望你能澄清我的疑问。再次感谢你,我是一个初学者,所以有时我看不到程序中的简单错误,我很高兴你指出了这一点。顺便说一下,我将Tc转换为一个整数,这样它就可以在循环中运行。ys=[1e10,h]是什么意思。图表无法运行,我只替换为ys=[h],我甚至尝试了您最初的建议ys=[h,h]。它也不能运行。我不知道如何识别错误。更改为
ys=[h]
。这是一次实验的残留物。顺便说一句,如果它解决了你的问题,你可以给出一个答案。无需将Tc
转换为整数。我们使用while循环,用t+=step
计算当前时间,直到到达Tc
。您好,您的第一个程序可以运行,但第二个程序无法运行。我一直在与我的合作伙伴一起尝试,但没有成功,请您帮忙好吗?“def init()”的作用是什么?
def get_y():
t = 0
while t <= Tc:
y = -0.5 * g * t**2 + h
yield y
t += step