diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 6fdf1265ff3b..b8b91e9a0dd4 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1471,6 +1471,9 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False, subplot_kw = {} if gridspec_kw is None: gridspec_kw = {} + # don't mutate kwargs passed by user... + subplot_kw = subplot_kw.copy() + gridspec_kw = gridspec_kw.copy() if self.get_constrained_layout(): gs = GridSpec(nrows, ncols, figure=self, **gridspec_kw) diff --git a/lib/matplotlib/tests/test_subplots.py b/lib/matplotlib/tests/test_subplots.py index 30cebe656e58..8c3558d10ca5 100644 --- a/lib/matplotlib/tests/test_subplots.py +++ b/lib/matplotlib/tests/test_subplots.py @@ -149,3 +149,12 @@ def test_get_gridspec(): # ahem, pretty trivial, but... fig, ax = plt.subplots() assert ax.get_subplotspec().get_gridspec() == ax.get_gridspec() + + +def test_dont_mutate_kwargs(): + subplot_kw = {'sharex': 'all'} + gridspec_kw = {'width_ratios': [1, 2]} + fig, ax = plt.subplots(1, 2, subplot_kw=subplot_kw, + gridspec_kw=gridspec_kw) + assert subplot_kw == {'sharex': 'all'} + assert gridspec_kw == {'width_ratios': [1, 2]}