I'm trying to create a plot that animates the line plot. I've created static versions of the same data happily, but nothing I do creates an animated plot. I have a dataframe could df_output that contains the dates as the index and different columns for the data itself. I've tried this which just creates an empty plot in Jupyter that doesn't update:
%matplotlib notebook
import matplotlib.animation as animation
def animate(i, data_lst):
data_lst.append(df_output.iloc[i,1])
ax.clear()
ax.plot(data_lst)
ax.set_ylim(0, max(data_lst))
ax.set_title('Test title')
ax.set_ylabel('Test label')
data_lst = []
fig, ax = plt.subplots()
ani = animation.FuncAnimation(fig, animate, frames=100, fargs=(data_lst), interval=100)
plt.show()
Am I missing something obvious? I tried the code in Spyder and it's suggesting that I'm not actually calling the matplotlib.animation library? Thanks!
CodePudding user response:
Yeah, this is a bit of a funny one, but ([])
is not a one-length tuple, ([],)
is. In the above replace fargs=(data_lst,)
is all you need to get it to work. Or just use fargs=[data_lst]
instead of a tuple.