Skip to content

Commit 3d17d95

Browse files
committed
Merge pull request #1919 from mdboom/max-num-figures
Issue warning if too many figures are open
2 parents 7574ae7 + 0433bd5 commit 3d17d95

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

doc/users/whats_new.rst

Lines changed: 13 additions & 1 deletion
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,19 @@ 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_open_warning = rcParams['figure.max_open_warning']
398+
399+
if (max_open_warning >= 1 and
400+
len(allnums) >= max_open_warning):
401+
warnings.warn(
402+
"More than %d figures have been opened. Figures "
403+
"created through the pyplot interface "
404+
"(`matplotlib.pyplot.figure`) are retained until "
405+
"explicitly closed and may consume too much memory. "
406+
"(To control this warning, see the rcParam "
407+
"`figure.max_num_figures`)." %
408+
max_open_warning, RuntimeWarning)
409+
397410
if get_backend().lower() == 'ps':
398411
dpi = 72
399412

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
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_open_warning': [20, validate_int],
683684

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

lib/matplotlib/tests/test_figure.py

Lines changed: 10 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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_open_warning : 20 # The maximum number of figures to open through
322+
# the pyplot interface before emitting a warning.
323+
# If less than one this feature is disabled.
321324

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

0 commit comments

Comments
 (0)