The Wayback Machine - https://web.archive.org/web/20230308114910/https://github.com/matplotlib/matplotlib/issues/20909
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Animation error message #20909

Closed
Gallywix-L opened this issue Aug 26, 2021 · 6 comments · Fixed by #20916
Closed

[Bug]: Animation error message #20909

Gallywix-L opened this issue Aug 26, 2021 · 6 comments · Fixed by #20916
Labels
Community support Users in need of help. Documentation Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@Gallywix-L
Copy link

Bug summary

once I executed my code by matplotlib.animation, The programe response this without any animation: D:\Program Files\Python\Python38\lib\site-packages\matplotlib\animation.py:973: UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation.

Code for reproduction

import matplotlib.animation
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import font_manager
import pandas as pd
from matplotlib.animation import FuncAnimation


def update(date):
    # ax.clear()
    # dg = df[["Country", date]].sort_values(by=date, ascending=True).tail(10)
    ax.barh(range(10), df[date])


# 创建画布和初始参数
fig, ax = plt.subplots(figsize=(10, 5.625), dpi=100)

# 准备数据
df = pd.read_csv(r".\csv_test.csv")

date_list = df.columns[1:].unique().tolist()
# print(date_list)

a = FuncAnimation(fig=fig, func=update, frames=date_list, interval=50)

Actual outcome

"D:\Program Files\Python\Python38\python.exe" "D:/pyCharm Project/Bar_chart_race/Barchart_race.py"
['2020/4/8', '2020/4/9', '2020/4/10', '2020/4/11', '2020/4/12']
3.4.3
D:\Program Files\Python\Python38\lib\site-packages\matplotlib\animation.py:973: UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation.

Process finished with exit code 0

Expected outcome

a live plot

Operating system

Windows

Matplotlib Version

3.4.3

Matplotlib Backend

No response

Python version

3.8.10

Jupyter version

No response

Other libraries

No response

Installation

No response

Conda channel

No response

@tacaswell
Copy link
Member

I beleive you are running this as python my_script.py or similar via an IDE?

If that is the case, then you either need to call plt.show() (which will bring up a blocking window to show your animation) or do a.save('out.mp4') (or similar) to save the animation to file.

What is happening is that after you last line you make a, but there is no more code to be run so Python says "right, my job is done!" and shuts the process down. As part of shutting down the interpreter Python tears down all of the objects that you created, including the Animation object, which in turn notes that you created it and never used it (prior to us adding that warning your Python process would have just quietly and successfully exited!).

I suspect the action here is to make the waning message clearer about how you would go about using the animation object (either calling plt.show() to get a window or a.save).

@tacaswell tacaswell added Documentation Good first issue Open a pull request against these issues if there are no active ones! labels Aug 26, 2021
@tacaswell tacaswell added this to the unassigned milestone Aug 26, 2021
@timhoffm
Copy link
Member

You have to output the animation. Either to screen (plt.show()) or to a file (a.save(...)).

@timhoffm timhoffm added Community support Users in need of help. Documentation Good first issue Open a pull request against these issues if there are no active ones! and removed Documentation Good first issue Open a pull request against these issues if there are no active ones! labels Aug 26, 2021
@timhoffm
Copy link
Member

@tacaswell I did not explicitly remove the documentation and Good first issue tags. It seems we've been simultaneously trying to tag and that confuses github 😏 .

@timhoffm
Copy link
Member

The warning message here was tailored to the error case where the Animation object is not stored in a variable and immediately deleted after being created, e.g.

FuncAnimation(...)
plt.show()

We can't distinguish between this case and the case of no show/save. So the error message would have to cover both.

timhoffm added a commit to timhoffm/matplotlib that referenced this issue Aug 26, 2021
@Gallywix-L
Copy link
Author

I beleive you are running this as python my_script.py or similar via an IDE?

If that is the case, then you either need to call plt.show() (which will bring up a blocking window to show your animation) or do a.save('out.mp4') (or similar) to save the animation to file.

What is happening is that after you last line you make a, but there is no more code to be run so Python says "right, my job is done!" and shuts the process down. As part of shutting down the interpreter Python tears down all of the objects that you created, including the Animation object, which in turn notes that you created it and never used it (prior to us adding that warning your Python process would have just quietly and successfully exited!).

I suspect the action here is to make the waning message clearer about how you would go about using the animation object (either calling plt.show() to get a window or a.save).

Thanks for your help and patience

@Gallywix-L
Copy link
Author

Gallywix-L commented Aug 27, 2021

You have to output the animation. Either to screen (plt.show()) or to a file (a.save(...)).

It worked, Thx

@QuLogic QuLogic modified the milestones: unassigned, v3.5.0 Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community support Users in need of help. Documentation Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants