简体   繁体   中英

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.

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?

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. Every time you plot something on figure, plt.cla() then clears it.

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 . You'd better explicitly declare fig and ax .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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