warning about too many open figures
Solution 1:
Use .clf
or .cla
on your figure object instead of creating a new figure. From @DavidZwicker
Assuming you have imported pyplot
as
import matplotlib.pyplot as plt
plt.cla()
clears an axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched.
plt.clf()
clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.
plt.close()
closes a window, which will be the current window, if not specified otherwise. plt.close('all')
will close all open figures.
The reason that del fig
does not work is that the pyplot
state-machine keeps a reference to the figure around (as it must if it is going to know what the 'current figure' is). This means that even if you delete your ref to the figure, there is at least one live ref, hence it will never be garbage collected.
Since I'm polling on the collective wisdom here for this answer, @JoeKington mentions in the comments that plt.close(fig)
will remove a specific figure instance from the pylab state machine (plt._pylab_helpers.Gcf) and allow it to be garbage collected.
Solution 2:
Here's a bit more detail to expand on Hooked's answer. When I first read that answer, I missed the instruction to call clf()
instead of creating a new figure. clf()
on its own doesn't help if you then go and create another figure.
Here's a trivial example that causes the warning:
from matplotlib import pyplot as plt, patches
import os
def main():
path = 'figures'
for i in range(21):
_fig, ax = plt.subplots()
x = range(3*i)
y = [n*n for n in x]
ax.add_patch(patches.Rectangle(xy=(i, 1), width=i, height=10))
plt.step(x, y, linewidth=2, where='mid')
figname = 'fig_{}.png'.format(i)
dest = os.path.join(path, figname)
plt.savefig(dest) # write image to file
plt.clf()
print('Done.')
main()
To avoid the warning, I have to pull the call to subplots()
outside the loop. In order to keep seeing the rectangles, I need to switch clf()
to cla()
. That clears the axis without removing the axis itself.
from matplotlib import pyplot as plt, patches
import os
def main():
path = 'figures'
_fig, ax = plt.subplots()
for i in range(21):
x = range(3*i)
y = [n*n for n in x]
ax.add_patch(patches.Rectangle(xy=(i, 1), width=i, height=10))
plt.step(x, y, linewidth=2, where='mid')
figname = 'fig_{}.png'.format(i)
dest = os.path.join(path, figname)
plt.savefig(dest) # write image to file
plt.cla()
print('Done.')
main()
If you're generating plots in batches, you might have to use both cla()
and close()
. I ran into a problem where a batch could have more than 20 plots without complaining, but it would complain after 20 batches. I fixed that by using cla()
after each plot, and close()
after each batch.
from matplotlib import pyplot as plt, patches
import os
def main():
for i in range(21):
print('Batch {}'.format(i))
make_plots('figures')
print('Done.')
def make_plots(path):
fig, ax = plt.subplots()
for i in range(21):
x = range(3 * i)
y = [n * n for n in x]
ax.add_patch(patches.Rectangle(xy=(i, 1), width=i, height=10))
plt.step(x, y, linewidth=2, where='mid')
figname = 'fig_{}.png'.format(i)
dest = os.path.join(path, figname)
plt.savefig(dest) # write image to file
plt.cla()
plt.close(fig)
main()
I measured the performance to see if it was worth reusing the figure within a batch, and this little sample program slowed from 41s to 49s (20% slower) when I just called close()
after every plot.
Solution 3:
If you intend to knowingly keep many plots in memory, but don't want to be warned about it, you can update your options prior to generating figures.
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0})
This will prevent the warning from being emitted without changing anything about the way memory is managed.
Solution 4:
This is also useful if you only want to temporarily suppress the warning:
import matplotlib.pyplot as plt
with plt.rc_context(rc={'figure.max_open_warning': 0}):
lots_of_plots()
Solution 5:
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0})
If you use this, you won’t get that error, and it is the simplest way to do that.