简体   繁体   English

Plot 未显示 animation matplotlib

[英]Plot not showing for animation matplotlib

I am trying to animate the numpy array C3, this is an array with one channel of electrode data and I want to plot it in real time using matplotlib.我正在尝试为 numpy 数组 C3 制作动画,这是一个具有一个电极数据通道的数组,我想使用 matplotlib 实时生成它 plot。

I have created my update function but nothing is printing out, I though the the syntax is you pass i through to loop through the plots and the FuncAnimation should do the rest. Can someone please point me in the right direction?我已经创建了我的更新 function 但没有打印出来,我虽然语法是你通过i通过情节和 FuncAnimation 应该做 rest。有人能给我指出正确的方向吗?

Much appreciated!非常感激!

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

data_skip = 50

def update_plot(i):
    plt.cla()
    
    plt.plot(C3[i:i+data_skip], t[i:i+data_skip])
    plt.scatter(C3[i], t[i], marker='o', color='r')
    
    plt.tight_layout()
    plt.show()
    
ani = FuncAnimation(plt.gcf(), update_plot, interval=1000)

plt.tight_layout()
plt.show()

Remove plt.cla() , it will clear current axes.删除plt.cla() ,它将清除当前轴。 Every time you plot something on figure, plt.cla() then clears it.每次你在图上输入 plot 时, plt.cla()清除它。

You could confirm it by the following minimul example.您可以通过以下最小示例进行确认。 It plots nothing它什么也没画

import matplotlib.pyplot as plt
import numpy as np

C3 = np.linspace(0.5, 10, 100)
t = np.linspace(0.5, 10, 100)

plt.plot(C3, t)

plt.cla()

plt.show()

Matplotlib documentation have an example to write animation code: simple_anim.py . Matplotlib 文档有写animation 代码的例子:simple_anim.py You'd better explicitly declare fig and ax .您最好明确声明figax

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

C3 = np.linspace(0.5, 10, 100)
t = np.linspace(0.5, 10, 100)

data_skip = 2

fig, ax = plt.subplots()


def update_plot(i):
    ax.plot(C3[i:i+data_skip], t[i:i+data_skip])
    ax.scatter(C3[i], t[i], marker='o', color='r')


ani = FuncAnimation(fig, update_plot, interval=1000)

plt.tight_layout()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM