From d41b04853af914752f6f653777810fca66fdd774 Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 29 Mar 2024 21:36:56 +0530 Subject: [PATCH 1/5] Add warning for multiple pyplot.figure calls with same ID Exclude the 'clear' argument --- lib/matplotlib/pyplot.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 3b1a01c28408..aa0a8e9cca5f 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -995,18 +995,24 @@ def figure( fig_label = '' if num is None: num = next_num - elif isinstance(num, str): - fig_label = num - all_labels = get_figlabels() - if fig_label not in all_labels: - if fig_label == 'all': - _api.warn_external("close('all') closes all existing figures.") - num = next_num - else: - inum = all_labels.index(fig_label) - num = allnums[inum] else: - num = int(num) # crude validation of num argument + if any([figsize, dpi, facecolor, edgecolor, not frameon, + kwargs]) and num in allnums: + _api.warn_external( + "Ignoring specified arguments in this call " + f"because figure with num: {num} already exists") + if isinstance(num, str): + fig_label = num + all_labels = get_figlabels() + if fig_label not in all_labels: + if fig_label == 'all': + _api.warn_external("close('all') closes all existing figures.") + num = next_num + else: + inum = all_labels.index(fig_label) + num = allnums[inum] + else: + num = int(num) # crude validation of num argument # Type of "num" has narrowed to int, but mypy can't quite see it manager = _pylab_helpers.Gcf.get_fig_manager(num) # type: ignore[arg-type] From a9e972384e22408463900679d1c873bbd702e2de Mon Sep 17 00:00:00 2001 From: Pranav Date: Wed, 8 May 2024 17:18:48 +0530 Subject: [PATCH 2/5] Added test for warning Made test more versatile --- lib/matplotlib/tests/test_pyplot.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index a077aede8f8b..9052367b5908 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -457,3 +457,12 @@ def test_figure_hook(): fig = plt.figure() assert fig._test_was_here + + +def test_multiple_same_figure_calls(): + fig = mpl.pyplot.figure(1, figsize=(1, 2)) + with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"): + fig2 = mpl.pyplot.figure(1, figsize=(3, 4)) + assert fig is fig2 + fig3 = mpl.pyplot.figure(1) # Checks for false warnings + assert fig is fig3 From 18eb3e7aa3bbeb5c4745e96ab87c20323e5c6d05 Mon Sep 17 00:00:00 2001 From: Pranav Date: Mon, 13 May 2024 08:14:46 +0530 Subject: [PATCH 3/5] Added test for uncovered line --- lib/matplotlib/tests/test_pyplot.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 9052367b5908..69c53f4d4163 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -466,3 +466,11 @@ def test_multiple_same_figure_calls(): assert fig is fig2 fig3 = mpl.pyplot.figure(1) # Checks for false warnings assert fig is fig3 + + +def test_close_all_warning(): + fig1 = plt.figure() + + # Check that the warning is issued when 'all' is passed to plt.figure + with pytest.warns(UserWarning, match="closes all existing figures"): + fig2 = plt.figure("all") From dd9cda0159fc367576bc0cc69f2b3e365234ba30 Mon Sep 17 00:00:00 2001 From: Pranav Date: Sun, 19 May 2024 22:43:21 +0530 Subject: [PATCH 4/5] Added warning for recurrent mention using figure object and tests --- lib/matplotlib/pyplot.py | 7 ++++++- lib/matplotlib/tests/test_pyplot.py | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index aa0a8e9cca5f..3fe2cfe2fd2b 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -983,14 +983,19 @@ def figure( `~matplotlib.rcParams` defines the default values, which can be modified in the matplotlibrc file. """ + allnums = get_fignums() + if isinstance(num, FigureBase): # type narrowed to `Figure | SubFigure` by combination of input and isinstance if num.canvas.manager is None: raise ValueError("The passed figure is not managed by pyplot") + elif num.canvas.manager.num in allnums: + _api.warn_external( + "Ignoring specified arguments in this call " + f"because figure with num: {num.canvas.manager.num} already exists") _pylab_helpers.Gcf.set_active(num.canvas.manager) return num.figure - allnums = get_fignums() next_num = max(allnums) + 1 if allnums else 1 fig_label = '' if num is None: diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 69c53f4d4163..63dc239df2e8 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -463,6 +463,8 @@ def test_multiple_same_figure_calls(): fig = mpl.pyplot.figure(1, figsize=(1, 2)) with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"): fig2 = mpl.pyplot.figure(1, figsize=(3, 4)) + with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"): + mpl.pyplot.figure(fig, figsize=(5, 6)) assert fig is fig2 fig3 = mpl.pyplot.figure(1) # Checks for false warnings assert fig is fig3 From af118523e245912dbccea0149b9f05f63a4a0505 Mon Sep 17 00:00:00 2001 From: Pranav Date: Sun, 19 May 2024 23:15:50 +0530 Subject: [PATCH 5/5] Minor Fix --- lib/matplotlib/pyplot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 3fe2cfe2fd2b..52850f128cae 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -989,7 +989,8 @@ def figure( # type narrowed to `Figure | SubFigure` by combination of input and isinstance if num.canvas.manager is None: raise ValueError("The passed figure is not managed by pyplot") - elif num.canvas.manager.num in allnums: + elif any([figsize, dpi, facecolor, edgecolor, not frameon, + kwargs]) and num.canvas.manager.num in allnums: _api.warn_external( "Ignoring specified arguments in this call " f"because figure with num: {num.canvas.manager.num} already exists")