Skip to content

Commit 8c8bc8f

Browse files
committed
Warn when too many figures are opened
1 parent 7574ae7 commit 8c8bc8f

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

doc/users/whats_new.rst

+13-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ rcParam has been set, and will not retroactively affect already
4545
existing text objects. This brings their behavior in line with most
4646
other rcParams.
4747

48+
Catch opening too many figures using pyplot
49+
-------------------------------------------
50+
Figures created through `pyplot.figure` are retained until they are
51+
explicitly closed. It is therefore common for new users of matplotlib
52+
to run out of memory when creating a large series of figures in a
53+
loop without closing them.
54+
55+
matplotlib will now display a `RuntimeWarning` when too many figures
56+
have been opened at once. By default, this is displayed for 20 or
57+
more figures, but the exact number may be controlled using the
58+
``figure.max_num_figures`` rcParam.
59+
4860
``axes.xmargin`` and ``axes.ymargin`` added to rcParams
4961
-------------------------------------------------------
5062
``rcParam`` values (``axes.xmargin`` and ``axes.ymargin``) were added
@@ -111,7 +123,7 @@ conversion (`mpl.rc('svg', fonttype='none')`).
111123
More robust boxplots
112124
--------------------
113125
Paul Hobson provided a fix to the :func:`~matplotlib.pyplot.boxplot`
114-
method that prevent whiskers from being drawn inside the box for
126+
method that prevent whiskers from being drawn inside the box for
115127
oddly distributed data sets.
116128

117129
Triangular grid interpolation

lib/matplotlib/pyplot.py

+11
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
394394

395395
figManager = _pylab_helpers.Gcf.get_fig_manager(num)
396396
if figManager is None:
397+
max_num_figures = rcParams['figure.max_num_figures']
398+
399+
if (max_num_figures >= 1 and
400+
len(allnums) >= max_num_figures):
401+
warnings.warn(
402+
"More than %d figures have been opened. Figures created "
403+
"through the pyplot interface are retained until explicitly "
404+
"closed. (To control this warning, see the rcParam "
405+
"'figure.max_num_figures'." %
406+
max_num_figures, RuntimeWarning)
407+
397408
if get_backend().lower() == 'ps':
398409
dpi = 72
399410

lib/matplotlib/rcsetup.py

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ def __call__(self, s):
680680
'figure.edgecolor': ['w', validate_color], # edgecolor; white
681681
'figure.frameon': [True, validate_bool],
682682
'figure.autolayout': [False, validate_bool],
683+
'figure.max_num_figures': [20, validate_int],
683684

684685
'figure.subplot.left': [0.125, ValidateInterval(0, 1, closedmin=True,
685686
closedmax=True)],

lib/matplotlib/tests/test_figure.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from nose.tools import assert_equal, assert_true
1+
from nose.tools import assert_equal, assert_true, assert_raises
22
from matplotlib.testing.decorators import image_comparison, cleanup
33
import matplotlib.pyplot as plt
44

@@ -92,6 +92,15 @@ def test_alpha():
9292
alpha=0.6,
9393
facecolor='red'))
9494

95+
@cleanup
96+
def test_too_many_figures():
97+
import warnings
98+
99+
with warnings.catch_warnings(record=True) as w:
100+
for i in range(22):
101+
fig = plt.figure()
102+
assert len(w) == 1
103+
95104

96105
if __name__ == "__main__":
97106
import nose

matplotlibrc.template

+3
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
318318
#figure.edgecolor : white # figure edgecolor
319319
#figure.autolayout : False # When True, automatically adjust subplot
320320
# parameters to make the plot fit the figure
321+
#figure.max_num_figures : 20 # The maximum number of figures to open through
322+
# the pyplot interface. If less than one,
323+
# there is no maximum.
321324

322325
# The figure subplot parameters. All dimensions are a fraction of the
323326
# figure width or height

0 commit comments

Comments
 (0)