Problem FunctionAnimation inside a def

Hello i’m trying to real time plot inside a program made with qt

the line are this:

def on_line(self):
   fig = plt.figure()
   ax = fig.add_subplot(1, 1 , 1)
   lista_x_vals = []
   lista_y_vals = []    


   def animate(self, i):
       lista_y_vals.append(self.y_vals)
       lista_x_vals.append(self.x_vals)
       ax.clear()
       ax.plot(lista_y_vals, lista_x_vals)
        

   ani = FuncAnimation(fig, animate, fargs = (lista_y_vals, lista_x_vals), interval = 500)
   plt.show()

but from what i understand the animate function is not called, can you help me to solve the problem

thanks in advance

Hello.

My very first mistake with FuncAnimation. And the solution is very simple: just return the ani variable. Whether it will be used further or not doesn’t matter.

As for the reason why it doesn’t work the way you expect, I think it is entirely related to the GC (garbage collector). I could be wrong in details, but the ani variable is “cleared” when the function completes its work, so there isn’t animation. As for why it was done such way, it is simple: to prevent a memory leak.