0

I tried to run the example code from following site in Python 3.8:

https://www.demo2s.com/python/python-matplotlib-animation-animate-decay.html

However, I get the following error:

C:\Users\username\anaconda3\lib\site-packages\matplotlib\animation.py:889: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. anim, that exists until you have outputted the Animation using plt.show() or anim.save(). warnings.warn(

Note that this is the full error that I get.

How can I solve this?

Here is the complete code:

import itertools

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def data_gen():# www  .de  m  o2  s.c o  m
    for cnt in itertools.count():
        t = cnt / 10
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)
plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Riemann
  • 158
  • 10
  • Please note that at StackOverflow linking to externally stored code is discouraged, as that page may disappear or be changed at any time, leaving an orphan question. Also, it is strongly recommended to add the full error trace instead of just the last lines. Are you sure you copied the code in its entirety? The warning seems to indicate that you left out `ani = ` in `ani = animation.FuncAnimation(....)`. Which matplotlib version are you running? – JohanC May 02 '22 at 13:52
  • @JohanC thanks for the comment! I updated the question. I am currently using matplotlib version 3.5.1. I am running it in Spyder. – Riemann May 02 '22 at 15:02
  • I am not sure whether I get an error or a warning, but anyway the code does not work. – Riemann May 02 '22 at 15:03
  • 1
    It works fine on my environment. – Davide_sd May 02 '22 at 15:09
  • So what could be wrong with my environment? – Riemann May 03 '22 at 15:56

0 Answers0