-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: allow colorbar mappable norm to change and do right thing #13234
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
Conversation
0300161
to
2af5f43
Compare
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.
Looks good.
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.
Looks good. Can you please add a note to the docstring of set_norm
that it will reset the formatter and locator?
This looks good! Thanks a lot for the detailed explanation. It made reviewing much faster. |
OK, note added, though its a bit of a funky place to add a note about colorbar changes.... |
lib/matplotlib/cm.py
Outdated
Notes | ||
----- | ||
If there are any colorbars using the mappable for this norm, setting | ||
the norm of the mappable will reeset the norm, locator, and formatters |
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.
Typo.
the norm of the mappable will reeset the norm, locator, and formatters | |
the norm of the mappable will reset the norm, locator, and formatters |
True, but better than nothing and I don't have a better solution. |
4356dcc
to
712aceb
Compare
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.
LGTM!
I went ahead and fixed the small PEP8 error that made the tests fail.
@jklymak I screwed up and didn't fix all the PEP8 from the github text editor… Do you mind fixing the rest (and maybe rebasing to squash the PEP8 fixes commits altogether?) |
Thanks for the review @NelleV. But this is still a WIP. I mucked around with so much it needs a thorough cleaning now. In particular for each colorbar draw_all is called three times which is unacceptable! |
Stale! Sorry for the extra work!
I think this is ready for now, but pinging @LevN0 Note that I decided we don't need to make locator and formatter properties, so I took that out to make things more straight forward... |
lib/matplotlib/colorbar.py
Outdated
@@ -387,30 +387,27 @@ def __init__(self, ax, cmap=None, | |||
self.outline = None | |||
self.patch = None | |||
self.dividers = None | |||
# self.locator and self.formatter are properties... |
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.
this comment is not true anymore, I think.
lib/matplotlib/colorbar.py
Outdated
@@ -449,14 +445,14 @@ def draw_all(self): | |||
if self.filled: | |||
self._add_solids(X, Y, C) | |||
|
|||
def set_norm(self, norm): | |||
""" | |||
set the norm of the mappable associateed with this 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.
set the norm of the mappable associateed with this colorbar. | |
Set the norm of the mappable associated with this colorbar. |
lib/matplotlib/colorbar.py
Outdated
@@ -1105,6 +1131,7 @@ def on_mappable_changed(self, mappable): | |||
by :func:`colorbar_factory` and should not be called manually. | |||
|
|||
""" | |||
_log.debug('mappable changed') |
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.
_log.debug('mappable changed') | |
_log.debug('colorbar mappable changed') |
Giving slightly more context helps understanding the log output.
lib/matplotlib/colorbar.py
Outdated
""" | ||
|
||
_log.debug('update normal') |
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.
_log.debug('update normal') | |
_log.debug('colorbar update normal') |
Giving slightly more context helps understanding the log output.
assert isinstance(cbar.locator, _ColorbarLogLocator) | ||
assert np.allclose(cbar.ax.yaxis.get_majorticklocs(), | ||
np.logspace(-8, 5, 14)) | ||
# note that that set_norm reemoves the FixedLocator... |
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.
# note that that set_norm reemoves the FixedLocator... | |
# note that set_norm removes the FixedLocator |
assert np.isclose(cbar.vmin, z.min() * 1000) | ||
assert np.isclose(cbar.vmax, z.max() * 1000) | ||
|
||
|
||
def test_colorbar_format(): | ||
# make sure that format is passed properly.... |
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.
# make sure that format is passed properly.... | |
# make sure that format is passed properly |
Sorry for the WIP embargo and confusion that caused. Most of the issues identified in #13228 have been addressed and this is ready for a second review. |
Is there a way to directly merge @timhoffm 's suggestion? |
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 like @timhoffm 's suggestions. Good to go once they are merged!
f6c3fd9
to
48b3510
Compare
00d62a7
to
0a8942d
Compare
I think this is ready again:
|
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.
It would be good to remove the reference to ScalarMappable inheritance in the tutorial:
https://matplotlib.org/tutorials/colors/colorbar_only.html#sphx-glr-tutorials-colors-colorbar-only-py
lib/matplotlib/colorbar.py
Outdated
Private class to hold deprecated ColorbarBase methods that used to be | ||
inhereted from ScalarMappable. | ||
""" | ||
@cbook.deprecated("3.1", alternative="mappable.set_norm") |
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.
For alternative, did you mean ScalarMappable.set_norm
here, 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.
Fixed both points! Thanks,
This is technically an API change, but hopefully not a controversial one (the inheritance from ScalarMappable didn't really work properly anyways...) |
4f616b2
to
447747c
Compare
Did anyone else have time to look at this again? Changes from previous approvals are here (squashed and rebased) |
FIX: stop colorbar from being a ScalarMappable, and deprecate common fcns FIX: allow norm limit to change and not change user-set ticks and format FIX: moved old ScalarMappable methods to a private method DOC: fix deprecation and tutorial DOC: api note
447747c
to
d7e098c
Compare
Probably not addressed to me, but I looked at it again to confirm it still addresses the original issue, and it does. |
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.
Looks good to me! I've added two minor comments on docstrings.
Thanks! |
Owee, I'm MrMeeseeks, Look at me. There seem to be a conflict, please backport manually. Here are approximate instructions:
And apply the correct labels and milestones. Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon! If these instruction are inaccurate, feel free to suggest an improvement. |
…anged FIX: allow colorbar mappable norm to change and do right thing
The dummy parent class introduced in matplotlib#13234 had a `set_clim` with a different signature to the version in `ScalarMappable`. This caused code which called it and passed both arguments to fail, rather than simply no-op.
PR Summary
Closes #13228
Before
Note this colorbar is simply wrong...
After
Changing norm erases colorbar formatter and locator...
If the norm is changed, then any formatter and locator settings are also lost. This is new behaviour, but I think reasonable if the user has changed the norm. Here we illustrate this just by setting the ticks (which changes the Locator to FixedLocator). Note that when the norm is set the locator goes back to the automatic locator.
Next issue: Reset scale if norm changes...
The axis scale wasn't being reset if the norm changed, but it needs to be reset as well...
Before
After
I'm marking WIP until @LevN0 lets us know we squashed all the bugs...
Next Issue: format='%1.4e' now works...
See new test...
PR Checklist