jbusecke / xmovie

A simple way of creating movies from xarray objects
https://xmovie.readthedocs.io/
MIT License
251 stars 38 forks source link

use the `Figure` constructor instead of `plt.figure` #159

Open keewis opened 8 months ago

keewis commented 8 months ago

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.