2

So I was following Corey Schafer's video tutorial link and got confused as to why we need to create a variable for the FuncAnimation Class.

Basically why I need to do this

ani = FuncAnimation(plt.gcf(), animate,interval=1000)

instead of this

FuncAnimation(plt.gcf(), animate,interval=1000)

The code works only if I create the variable,and I am just confused as to why we need create it in the first place, especially since it is not used. All the other code examples Ive seen that use the FuncAnimation class also set it to a variable.If I could get some clarification on why its necessary it would be greatly appreciated.


import random
from itertools import count
import pandas as pd 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation
import matplotlib
plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []


index = count()

def animate(i):
    x_vals.append(next(index))
    y_vals.append(random.randint(0, 5))

    plt.cla()

    plt.plot(x_vals,y_vals)



ani = FuncAnimation(plt.gcf(), animate,interval=1000)

plt.tight_layout()
plt.show()

My intuition makes me think this should work, but instead nothing get plotted


import random
from itertools import count
import pandas as pd 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation
import matplotlib
plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []


index = count()

def animate(i):
    x_vals.append(next(index))
    y_vals.append(random.randint(0, 5))

    plt.cla()

    plt.plot(x_vals,y_vals)



FuncAnimation(plt.gcf(), animate,interval=1000)

plt.tight_layout()
plt.show()
Bill
  • 21
  • 3

0 Answers0