API: Raise ValueError in subplots if num refers to existing figure · matplotlib/matplotlib@06ab8ab · GitHub
Skip to content

Commit 06ab8ab

Browse files
API: Raise ValueError in subplots if num refers to existing figure
1 parent e3d6dfc commit 06ab8ab

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,13 @@ def subplots(
18561856
fig, ax = plt.subplots(num=10, clear=True)
18571857
18581858
"""
1859+
num = fig_kw.get('num')
1860+
if num is not None and fignum_exists(num):
1861+
if not fig_kw.get('clear'):
1862+
raise ValueError(
1863+
f"Figure {num!r} already exists. Use plt.figure({num!r}) "
1864+
"to get it or plt.close({num!r}) to close it. "
1865+
"Alternatively, pass 'clear=True' to subplots().")
18591866
fig = figure(**fig_kw)
18601867
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
18611868
squeeze=squeeze, subplot_kw=subplot_kw,

lib/matplotlib/tests/test_pyplot.py

Lines changed: 16 additions & 0 deletions

0 commit comments

Comments
 (0)