-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX/ENH CL: Allow single parent colorbar w/ gridspec layout #10629
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
FIX/ENH CL: Allow single parent colorbar w/ gridspec layout #10629
Conversation
…idspec or subplotspec
Codecov Report
@@ Coverage Diff @@
## master #10629 +/- ##
==========================================
+ Coverage 73.44% 73.44% +<.01%
==========================================
Files 283 283
Lines 71729 71730 +1
Branches 10322 10322
==========================================
+ Hits 52678 52679 +1
Misses 16136 16136
Partials 2915 2915
Continue to review full report at Codecov.
|
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.
The implementation looks good to me and this API change¹ seems to solve the surprising behavior reported in #10627.
But I would love to hear from a core dev with a bit more insights than I can provide on the right API for this 🐑.
¹ : which is a bit virtual as this is an experimental API ;)
lib/matplotlib/axes/_subplots.py
Outdated
@@ -173,7 +173,9 @@ def _make_twin_axes(self, *kl, **kwargs): | |||
""" | |||
from matplotlib.projections import process_projection_requirements | |||
if 'sharex' in kwargs and 'sharey' in kwargs: | |||
raise ValueError("Twinned Axes may share only one axis.") | |||
if (not (kwargs['sharex'] == self ) and |
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.
Extra space before the parenthesis == self )
: PEP8 seems to be unhappy.
lib/matplotlib/axes/_subplots.py
Outdated
raise ValueError("Twinned Axes may share only one axis.") | ||
if (not (kwargs['sharex'] == self ) and | ||
not (kwargs['sharex'] == self)): | ||
raise ValueError("Twinned Axes may share only one axis.") |
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.
Are those line a trial for a 2-in-1 PR done on purpose regarding to #10585 ?
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.
Ooops, this shouldn't have been in this PR....
@@ -105,7 +105,7 @@ def example_plot(ax, fontsize=12, nodec=False): | |||
fig.colorbar(im, ax=ax, shrink=0.6) | |||
|
|||
############################################################################ | |||
# If you specify multiple axes to the ``ax`` argument of ``colorbar``, | |||
# If you specify a list of axes to the ``ax`` argument of ``colorbar``, |
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.
If I understood correctly, even a tuple or an ndarray (or actually any kind of iterable...) will trigger that behavior, will it not? Should this be briefly mentioned or would it make the API even harder to understand :/?
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.
Changed to a list (or other iterable container)
|
||
############################################################################ | ||
# The API to make a single-axes behave like a list of axes is to specify | ||
# it as a list, as below: |
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.
Similar “remark” about the word "list" (vs. a more general term to mean any kind of sequence) than above. TBH I do not have a clear opinion on this, but just wanted to make sure that one noticed the small subtlety.
e729962
to
acc4e17
Compare
Thanks for the review @afvincent. Just to be clear about the API thing - I'm more than happy to have a different API for this, as this is a bit subtle. OTOH, it doesn't affect non-constrained_layout plots, so its a self-contained change. And because the API is experimental, if we don't like it, we can change it 😉 |
Well, I have been too much exposed to Matplotlib and to its internals not to be biased, but |
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.
Makes sense to me.
Backport PR #10629 on branch v2.2.x
PR Summary
In the master version of constrained_layout (CL), a colorbar associated with a single parent axes (i.e.
fig.colorbar(im, ax=axs[2])
) is associated with the subplotspec layout box. If there is more than one parent, it is associated with the gridspec layout box that contains the subplotspecs (i.e.fig.colorbar(im, ax=axs[:2])
).As noted in #10627 by @afvincent this leads to a surprising result for a layout like:
yields:
This is technically correct. We want to be able to have a single colorbar follow a single axes:
However, for this case it is undesirable. This PR changes the argument of
ax
infig.colorbar
so that if it is an iterable, even with just one element, it will be treated as a gridspec level colorbar. i.e. if instead offig.colorbar(pc, ax=axs[2])
we make the axes a list:we get:
(the user will still have to futz around w/ the colorbar aspect ratios to make that look nice).
This doesn't break
constrained_layout=False
(default), which both yeild identical layouts:so this is a non-breaking change...
The todo list below is still active. I'll add a test and update the tutorial if this approach is OK.Decided against a dedicated test. The underlying functionality works, so a test is a bit superfluous. Did update the tutorial.
PR Checklist