Skip to content
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

Merged
merged 1 commit into from
Feb 17, 2024

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Dec 24, 2023

In https://stackoverflow.com/questions/77710768/getting-constrained-layout-to-work-with-nested-subplots-in-matplotlib
the user did

    fig, axs = plt.subplots(1, 2)
    gs = mpl.gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=axs[0])

which isn't crazy, though subplot_spec should really be of type SubplotSpec. Indeed it works fine if the user doesn't call constrained_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 type SubplotSpec.

@anntzer
Copy link
Contributor

anntzer commented Dec 24, 2023

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?

@jklymak
Copy link
Member Author

jklymak commented Dec 24, 2023

No absolutely not. OTOH you can see how they made the error, so it's easy to be a bit more forgiving.

@timhoffm
Copy link
Member

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?“).

@jklymak
Copy link
Member Author

jklymak commented Dec 25, 2023

We could do that instead. OTOH I wasn't suggesting we document this as working.

@timhoffm
Copy link
Member

timhoffm commented Dec 25, 2023

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.
More generally, people rely on observable behavior, not on documentation (c.f. Hyrum‘s Law). So, whether we document or not is not relevant for what people expect. The actual behavior is the de-facto leading measure. For clarity and consistency, we should strive to reflect that behavior in the documentation. Undocumented behavior is not a feature but either deficiency in our documentation or actually unwanted behavior.

@jklymak jklymak force-pushed the mnt-arghandling-subplotspec branch 2 times, most recently from 2459e42 to 4795d2d Compare December 26, 2023 16:52
@jklymak
Copy link
Member Author

jklymak commented Dec 26, 2023

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:
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@rcomer rcomer added this to the v3.9.0 milestone Dec 29, 2023
@@ -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?

@github-actions github-actions bot added the topic: geometry manager LayoutEngine, Constrained layout, Tight layout label Feb 16, 2024
@rcomer rcomer merged commit b104725 into matplotlib:main Feb 17, 2024
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API: argument checking topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants