Python 将阵列作为输入使用matplotlib动画

Python 将阵列作为输入使用matplotlib动画,python,matplotlib,Python,Matplotlib,我正在用python制作一个交互式偏微分方程求解器(我可以随时绘制或修改字段) 解算器和分析器已准备就绪。我只需要想象一下。我拿了一个示例代码,我做了这个玩具代码。它可以设置数据动画,并通过鼠标移动对其进行修改 import matplotlib.pyplot as plt from numpy import * import matplotlib.animation as animation dx = 0.1 X=arange(0,10,dx) #function to plot Y =

我正在用python制作一个交互式偏微分方程求解器(我可以随时绘制或修改字段)

解算器和分析器已准备就绪。我只需要想象一下。我拿了一个示例代码,我做了这个玩具代码。它可以设置数据动画,并通过鼠标移动对其进行修改

import matplotlib.pyplot as plt
from numpy import *
import matplotlib.animation as animation

dx = 0.1
X=arange(0,10,dx)

#function to plot
Y = sin(pi*X)
pause = False
#a artificial simulation, the field is move rigidly
def simData():
    global Y
    t_max = 10000
    for t in range(t_max):
        if not pause:
            Y = roll(Y,1)
        yield Y

#detect the position when mouse click and fix the field at this position
def on_click(event):
    global pause,Y,movement
    pause = True
    index = int(event.xdata/dx)
    Y[index] =  event.ydata

#
def on_release(event):
    global pause
    pause = False

#detect the position when mouse is moving and is clicked and fix the field at this position
def on_move(event):
   global pause
   if pause:
    index = int(event.xdata/0.1)
    Y[index] =  event.ydata

def simPoints(simData):
    x = simData
    line.set_ydata(x)
    return line

#initial plot
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(X, Y, 'b', ms=10) 
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)

#conecting the events
fig.canvas.mpl_connect('button_press_event', on_click)
fig.canvas.mpl_connect('button_release_event', on_release)
fig.canvas.mpl_connect('motion_notify_event', on_move)

#starting the animation
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
    repeat=True)
plt.show()
但是,函数动画需要一个包含数据的生成器,我不能使用外部函数独立于动画生成数据(我需要在模拟仍在运行的情况下关闭动画)。我该怎么做

(模拟场U(X)所需的结构类似于:

...
for t in steps:
  U = equation.integrate(U,t)
  if i_want_to_calculate: calculing_important_values(U)
  if i_want_to_visualize: screen.animate(U)
finishing_the_simulation()

谢谢!!!(请原谅我的英语)

这取决于方程的速度。积分(U,t)。例如,如果您可以每秒计算100帧,而一个动画只需要每秒20帧。那么您可以制作一个多进程应用程序,一个进程进行模拟,并将数据发送到另一个进程以显示动画。谢谢您的回答。这是一个想法,但是animation.FuncAnimation使用生成器来执行nimation,但是,我只想使用数组作为输入,并使动画更新屏幕。animation.FuncAnimation需要调用后生成的所有帧,我需要逐个传递帧。