Mplfinance: Bug Report: No close plot for savefig, error 20 open plot's

Created on 11 May 2020  Â·  8Comments  Â·  Source: matplotlib/mplfinance

mpf.plot(df, type='candle', mav=(100, ), style='charles', title="EURUSD", savefig='save.png')

after> 20 times leads to an error that 20 charts are open

mplfinanceplotting.py:226: RuntimeWarning: More than 20 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).
fig = plt.figure()

enhancement released

All 8 comments

This is not a bug, but is a matplotlib feature intended to prevent leaking memory.

We have the same issue when running mplfinance regression tests. The solution is to use the returnfig kwarg and close the figure as done here.

In the future, we will consider adding kwarg closefig=True

Perhaps when user sets savefig and does not set returnfig, then we should close the Figure by default? Or should we prefer the user to be explicit with closefig=True? Thoughts?

I think it's worth closing by default

For the next release, I have added a closefig kwarg.
The default value will be 'auto', which means mpf.plot() will attempt to determine when it is "safe" to close the figure. This default behavior can be overridden by passing closefig=True or closefig=False.

At this time, it is unsafe or inappropriate to close the Figure if

  • returnfig=True or
  • block=False or
  • user has passed in their own Figure and Axes

Otherwise it is considered safe to close the Figure. With this enhancement, most users who want to create more than 20 Figures via mpf.plot() will not have to import pyplot and close the Figure themselves.

I am wondering how I can close the Figure by myself as an intermediate solution?

I expect to merge in this enhancement (closing the figure) sometime tomorrow (just finishing up the documentation now).

In the meantime, the short term solution is to have mpf.plot() return the figure and close it:

import matplotlib.pyplot as plt
...
fig, ax = mpf.plot(data, ... ,returnfig=True)
plt.close(fig)

Grreat, in the meantime i use plt.close(all)

new version 0.12.5 just released to Pypi automatically closes plots when "appropriate" and contains kwarg closefig which can be set to "auto" (default), True, or False.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leochan007 picture leochan007  Â·  6Comments

viorell91 picture viorell91  Â·  3Comments

simonholzapfel picture simonholzapfel  Â·  5Comments

schorschie picture schorschie  Â·  3Comments

ghost355 picture ghost355  Â·  5Comments