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

use the Figure constructor instead of plt.figure #159

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

keewis
Copy link

@keewis keewis commented Mar 5, 2024

With more than a certain (configurable) amount of figures, matplotlib emits this warning:

More than {max_open_warning} figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning). Consider using matplotlib.pyplot.close().

With lots of figures or big figures, we're thus more likely to run out of memory.

As far as I can tell, we have two options: explicitly close them using plt.close as the warning suggests, or create the figure using the constructor. Since it gets rid of global state and is simpler to get right if creating figures in parallel, in this PR I decided to do the latter (but we could certainly also go the other way).

This gets rid of the warning, but we now need to return the figure from .preview to be able to display it in a notebook. Otherwise, if we want to keep using pyplot for that we need to somehow register the figure with the figure manager in that function, but I couldn't figure out how to do so using the public API.

keewis and others added 5 commits March 4, 2024 17:59
`plt.figure` retains a copy all figure objects it created, which means
that the maximum memory requirement rises with the number of figures.
Otherwise `pytest` will try to execute it and complain about `return`
instead of `assert`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant