diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 8f8f9544677e..94253aefa3cc 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1232,6 +1232,10 @@ def subplot(*args, **kwargs): # If no existing axes match, then create a new one. if ax is None: ax = fig.add_subplot(*args, **kwargs) + elif kwargs: + _api.warn_external(f"An Axes with subplot spec {args} already exists. " + "The existing Axes will be returned, with all " + "keyword arguments ignored.") bbox = ax.bbox axes_to_delete = [] diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 6b520aea30d4..1cfcb0d0035f 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -153,3 +153,11 @@ def test_nested_ion_ioff(): with plt.ioff(): plt.ion() assert not mpl.is_interactive() + + +def test_subplot_warning(): + ax1 = plt.subplot(2, 1, 1) + with pytest.warns(UserWarning, match='An Axes with subplot spec ' + r'\(2, 1, 1\) already exists'): + ax2 = plt.subplot(2, 1, 1, polar=True) + assert ax1 is ax2