1

基本上我想做的是实时绘制一个图表,更新/动画跨多个进程共享的数据。我使用了 python 多处理模块中的 Manager 并共享了一个wave列表(即一个数组),该列表在while 循环中的另一个进程中更新/修改。

看来,当我运行 FuncAnimation 函数时,我只能根据同一进程中的局部或全局变量更新显示的数据。这是我的代码:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = np.arange(0, 4408)
wave = np.arange(0, 4408)

def animate(i):
    plt.cla()
    global x
    x = [a + 4408 for a in x] # x-axis continues to shift

    plt.plot(x, wave)

# wave array from another process
def graph(shared_wave):
    global wave
    wave = shared_wave # Here it gets updated once but never again
    ani = FuncAnimation(plt.gcf(), animate, interval=100)
    plt.tight_layout()
    plt.show()

任何帮助将不胜感激。我一直在考虑的一件事是在一个进程中使用多线程在一个线程中运行动画,并在另一个线程中更新全局变量。

4

0 回答 0