8000 MNT: arghandling subplotspec by jklymak · Pull Request #27565 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MNT: arghandling subplotspec #27565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
7 changes: 6 additions & 1 deletion lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,12 @@ def __init__(self, nrows, ncols,
"""
self._wspace = wspace
self._hspace = hspace
self._subplot_spec = subplot_spec
if isinstance(subplot_spec, SubplotSpec):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use _api.check_isinstance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that existed. However, that issues a generic type error, whereas we want to provide a bit of guidance here?

self._subplot_spec = subplot_spec
else:
raise TypeError(
"subplot_spec must be type SubplotSpec, "
"usually from GridSpec, or axes.get_subplotspec.")
self.figure = self._subplot_spec.get_gridspec().figure
super().__init__(nrows, ncols,
width_ratios=width_ratios,
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_gridspec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pytest


Expand Down Expand Up @@ -35,3 +36,15 @@ def test_repr():
width_ratios=(1, 3))
assert repr(ss) == \
"GridSpec(2, 2, height_ratios=(3, 1), width_ratios=(1, 3))"


def test_subplotspec_args():
fig, axs = plt.subplots(1, 2)
# should work:
gs = gridspec.GridSpecFromSubplotSpec(2, 1,
subplot_spec=axs[0].get_subplotspec())
assert gs.get_topmost_subplotspec() == axs[0].get_subplotspec()
with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"):
gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs[0])
with pytest.raises(TypeError, match="subplot_spec must be type SubplotSpec"):
gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs)
0