-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
MNT: arghandling subplotspec #27565
MNT: arghandling subplotspec #27565
Conversation
01ed28d
to
db511e8
Compare
From the SO post, I assume the user would really also want this to auto-remove the previously existing axes, which I don't think this is something we want to do automatically? |
No absolutely not. OTOH you can see how they made the error, so it's easy to be a bit more forgiving. |
I‘m all for making API intuitive and forgiving. However, I‘m concerned that not clearly distinguishing between subplotspec and Axes dilutes the concepts. I would be more in favor returning a helpful error message („… did you mean Axes.subplot_spec?“). |
We could do that instead. OTOH I wasn't suggesting we document this as working. |
Uh, I see it as highly problematic to actively introduce undocumented behavior. The motivation for introducing the behavior would be that „it just works“, but that means we‘re implicitly giving API guarantees. It would be bad style to support this and take it away later by claiming „this has not been documented to work“. If we want to support something, we should document it. |
2459e42
to
4795d2d
Compare
OK, fair enough. This is obscure enough that we should just error rather than documenting. |
gs = gridspec.GridSpecFromSubplotSpec(2, 1, | ||
subplot_spec=axs[0].get_subplotspec()) | ||
assert gs.get_topmost_subplotspec() == axs[0].get_subplotspec() | ||
# this is a mistake, and not what the type hints give, but we allow: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment from an earlier revision? It says we allow, but then the code asserts an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -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): |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
4795d2d
to
77c6325
Compare
In https://stackoverflow.com/questions/77710768/getting-constrained-layout-to-work-with-nested-subplots-in-matplotlib
the user did
which isn't crazy, though
subplot_spec
should really be of typeSubplotSpec
. Indeed it works fine if the user doesn't callconstrained_layout
.This PR makes the argument a bit more forgiving and will do
axs[0].get_subplotspec
for the user without complaining, and will check for any other inputs not being typeSubplotSpec
.