-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Support subplots((m, n), ...) as shorthand for subplots(m, n, squeeze=False, ...) #19463
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
Comments
I empathize with the overall problem and this isn't a terrible solution. But I prefer your initial proposal and wonder if we could find a way to deprecate the auto-squeeze for subplot(1,3). |
Let's collect other devs' opinions for now. |
The current API of
|
The first one I nearly always spell
I can only say that I run into this problem quite often :(
I think
so that could easily be turned into
(and yes we'd need to allow grabbing
Actually I do like the idea too, but that would be quite a bit more work to implement. |
The documentation must still cover On a more general note, I don't quite like having redundant
If for a start we just make it a grid container, that is largely compatible with |
Just wanted to drop a comment if the signature ends up reworked one way or another that the fact that it's |
Graphics are almost always specified width times height. Matrices are almost always specified rows times columns. You can argue its internally inconsistent, but it is consistent with long-standing conventions. |
In retrospect, not flipping the default of squeeze when we pulled |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Even though we have no agreed on an alternative/better solution, I propose to close this as not intuitive and too clever. |
The idea stays here anyways, I agree this doesn't need to start open. |
Problem
subplots()
defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely for practicality, but this makes dynamic layouts pretty annoying: if you writeaxs = fig.subplots(m, n)
wherem
andn
are somehow dynamically computed (based on some dataset characteristics) and then later plan to useaxs[i, j]
, you basically have an IndexError waiting to happen unless you can be certain thatm
andn
are never 1. Of course one can remember to always writesubplots(..., squeeze=False)
, but that's a bit verbose.Proposed Solution
My preferred solution would probably have been to make the default
m
andn
not 1, but None, so that None means "1 in that direction, and squeeze that direction" whereas 1 just means 1 (without squeezing). This would have fixed most common cases, e.g. if you writesubplots()
you get a single axes,subplots(3)
orsubplots(nrows=3)
orsubplots(ncols=3)
you get a 1D array,subplots(m, n)
(wherem
andn
are dynamically computed variables) you always get a 2D array. I think in practice the main back-incompatibility of this proposal that occurs in real life issubplots(1, 3)
(single row, likely not so uncommon), which would typically have to be writtensubplots(None, 3)
orsubplots(ncols=3)
orfig, (axs,) = subplots(3)
(for the additional unpacking).Assuming that this backcompat breakage is not acceptable (it likely is not), one alternative would be to change the signature of
subplots()
to also support taking a single 2-tuple as argument (rather thannrows
andncols
separately, and makesubplots((m, n))
never squeeze the input. (Likely we wouldn't bother supportingm
orn
being None, in that case.) Then it would be OK to retrain my muscle memory to just always add an extra pair of parentheses when callingsubplots
.Additional context and prior art
The text was updated successfully, but these errors were encountered: