skip to Main Content

I have just written this code for the last project in school.
It works in Visual Studio, but when running the code there I get this errormessage (even though the program runs fine).

c:main.py:193: UserWarning: frames=None which we can infer the length of, did not pass an explicit *save_count* and passed cache_frame_data=True.  To avoid a possibly unbounded cache, frame data caching has been disabled. To suppress this warning either pass `cache_frame_data=False` or `save_count=MAX_FRAMES`.
  ani = FuncAnimation(plt.gcf(), func=animate, fargs=([velocity*math.cos(angle), velocity*math.sin(angle)],), interval=UPDRATE)

However, I guess that I have to present the code in Jupyter. When I copy my code there, the program doesn’t run at all. I get the error message:

/opt/conda/lib/python3.11/site-packages/matplotlib/animation.py:872: 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 output the Animation using `plt.show()` or `anim.save()`.
  warnings.warn(

I have no idea how to interpret this errormessage. Can you please explain a solution?

Part of code attached:

def animate(i, velocities):
    # Do things
    
def main():
    # Declare and call the animation function
    ani = FuncAnimation(plt.gcf(), func=animate, fargs=([velocity*math.cos(angle), velocity*math.sin(angle)],), interval=UPDRATE)
    plt.show()
    
main()

2

Answers


  1. Your animation object ani is being garbage collected because it’s defined inside main(), which means that it doesn’t persist after the function exits, which should be causing the error. I think moving ani to a global scope (putting it outside any functions) should be enough to solve the issue.

    For example:

    from matplotlib.animation import FuncAnimation
    import matplotlib.pyplot as plt
    from IPython.display import HTML
    import math
    
    def animate(i, velocities):
        # Your animation code here
        pass;
    
    velocity = 10;
    angle = math.radians(45);
    UPDRATE = 100;
    
    # Create the animation object in global scope!
    ani = FuncAnimation(
        plt.gcf(),
        func=animate,
        fargs=([velocity * math.cos(angle), velocity * math.sin(angle)],),
        interval=UPDRATE);
    
    # Display the animation in Jupyter Notebook
    HTML(ani.to_jshtml())
    
    Login or Signup to reply.
  2. You can try to add ‘cache_frame_data=False’ to your ‘ani’:

    ani = FuncAnimation(plt.gcf(), func=animate, fargs=([velocitymath.cos(angle), velocitymath.sin(angle)],), interval=UPDRATE , cache_frame_data=False)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search