Skip to content

Commit

Permalink
Merge pull request #24229 from tacaswell/fix_gridspec_input_mutation
Browse files Browse the repository at this point in the history
FIX: do not mutate dictionaries passed in by user
  • Loading branch information
anntzer committed Oct 20, 2022
2 parents d6e2548 + 9e5436f commit 000abdc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/matplotlib/figure.py
Expand Up @@ -878,8 +878,7 @@ def subplots(self, nrows=1, ncols=1, *, sharex=False, sharey=False,
# Note that this is the same as
fig.subplots(2, 2, sharex=True, sharey=True)
"""
if gridspec_kw is None:
gridspec_kw = {}
gridspec_kw = dict(gridspec_kw or {})
if height_ratios is not None:
if 'height_ratios' in gridspec_kw:
raise ValueError("'height_ratios' must not be defined both as "
Expand Down Expand Up @@ -1869,7 +1868,7 @@ def subplot_mosaic(self, mosaic, *, sharex=False, sharey=False,
"""
subplot_kw = subplot_kw or {}
gridspec_kw = gridspec_kw or {}
gridspec_kw = dict(gridspec_kw or {})
if height_ratios is not None:
if 'height_ratios' in gridspec_kw:
raise ValueError("'height_ratios' must not be defined both as "
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Expand Up @@ -1412,3 +1412,11 @@ def test_unpickle_with_device_pixel_ratio():
assert fig.dpi == 42*7
fig2 = pickle.loads(pickle.dumps(fig))
assert fig2.dpi == 42


def test_gridspec_no_mutate_input():
gs = {'left': .1}
gs_orig = dict(gs)
plt.subplots(1, 2, width_ratios=[1, 2], gridspec_kw=gs)
assert gs == gs_orig
plt.subplot_mosaic('AB', width_ratios=[1, 2], gridspec_kw=gs)

0 comments on commit 000abdc

Please sign in to comment.