jeudi 2 juillet 2020

Extra Frame in matplotlib.animation.FuncAnimate

I am exploring live graphs with matplotlib animations. Currently, I'm just adding a random integer to an array, and having my plot update based on the new number. The issue is that I seem to be getting an extra value drawn to the plot.

I have the following code:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

xs = []
ys = []

def animate(i, xs, ys):
    
    x = i + 1
    y = random.randint(1, 50)
    
    print '{}: {}, {}'.format(i, x, y)
    
    xs.append(x)
    ys.append(y)
            
    ax1.clear()
    ax1.plot(xs, ys)
        
ani = animation.FuncAnimation(fig,
                              animate,
                              frames = 10,
                              fargs = (xs, ys),
                              repeat = False)
plt.show()

I only want 10 values to be drawn, so I set frames = 10 in the FuncAnimate call. However, the print statement is outputting an 11th value:

Plot Result

Print Statement Output

So it's clear that 11 frames are being generated, rather than 10. Looking at the documenation for FuncAnimate, I can't see a reason why this would be happening.

Can anyone tell me what I'm missing?

Thanks!




Aucun commentaire:

Enregistrer un commentaire