Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix twoslopenorm colorbar #20582

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

Merged
merged 2 commits into from
Jul 13, 2021
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Jul 6, 2021

PR Summary

Closes #20581

#20054 makes it so the scale on a colorbar is set via the norm, and the inverse of its norm. The problem with scales, and tickers, is that they seek ticks beyond the min/max of their axis. PreviouslyTwoSlopeNorm linearly interpolated between vmin and vcenter and vcenter and vmax, but anything beyond vmax or less than vmin was also mapped to 0 and 1. So in the broken example a tick was sought at -1000, with vmin =-500. The norm processed this as 0 instead of a negative number, and hence it showed up on the axis.

Here we fix that by simply doing some linear extrapolation on the TwoSlopeNorm and its inverse. np.interp doesn't have linear extrapolation by default, and we can't use scipy, so just do it by hand. (yes I know that vcenter - 2 * (vcenter - vmin) = 2 * vmin - vcenter, but that is not as easy to parse).

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@jklymak jklymak added Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: color/colorbar topic: color/color & colormaps labels Jul 6, 2021
@jklymak jklymak added this to the v3.5.0 milestone Jul 6, 2021
@jklymak jklymak force-pushed the fix-twoslopenorm-colorbar branch from bd4102c to 78ddee0 Compare July 6, 2021 15:46
@QuLogic
Copy link
Member

QuLogic commented Jul 7, 2021

(yes I know that vcenter - 2 * (vcenter - vmin) = 2 * vmin - vcenter, but that is not as easy to parse).

You could also write that vmin - (vcenter - vmin), which is not as simplified, but maybe still easy to parse?

@jklymak jklymak force-pushed the fix-twoslopenorm-colorbar branch from 78ddee0 to 9ef1caa Compare July 7, 2021 13:56
@greglucas
Copy link
Contributor

As a general question, do you actually want to add extrapolation to these functions, or should we just set the left/right bounds to -inf/+inf to not make any assumptions beyond the data interval?

np.interp([self.vmin, self.vcenter, self.vmax], [0, 0.5, 1], left=-np.inf, right=np.inf)

@jklymak jklymak force-pushed the fix-twoslopenorm-colorbar branch from 266eac6 to a2f5a77 Compare July 7, 2021 21:12
@jklymak
Copy link
Member Author

jklymak commented Jul 7, 2021

@greglucas Yep, you are right, that works as well and is more elegant.

@jklymak jklymak force-pushed the fix-twoslopenorm-colorbar branch from a2f5a77 to 38ad08e Compare July 7, 2021 21:14
Comment on lines +316 to +317
# defining your own norm. Note for the colorbar to work, you must
# define an inverse for your norm:
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would make sense to create an abstract base class for Norms that raises NotImplemented for inverse() so that it doesn't accidentally work with a colorbar? (I think right now it is inheriting the inverse from the standard "Linear" Normalize class. We could catch the NotImplemented in the Colorbar code and warn appropriately there that the inverse method needs to be implemented.

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 think that would be sensible.

Copy link
Member

Choose a reason for hiding this comment

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

#7320 seems related to that.

@QuLogic
Copy link
Member

QuLogic commented Jul 8, 2021

I feel like the custom norm example never intended the 0 to be in the middle; maybe it needs to be written another way?

@jklymak
Copy link
Member Author

jklymak commented Jul 8, 2021

I wrote that example and indeed it's pretty common for water depths to have a different vertical scale than land relief.

If you want the size of the color ranges to be proportionate you would make the color map disproportionate with more land colours than ocean colours. But that is the opposite of the desired effect here.

@QuLogic
Copy link
Member

QuLogic commented Jul 12, 2021

Ah, I see that was mentioned in the TwoSlopeNorm section, but not the custom norm one. Perhaps that could be re-iterated there? As I just skipped directly to that section without reading the rest and didn't know it was intentional that way.

@jklymak
Copy link
Member Author

jklymak commented Jul 13, 2021

The first sentence of the example says that it is the same as the TwoSlopeNorm example?

@QuLogic
Copy link
Member

QuLogic commented Jul 13, 2021

Or I'm just blind today...

@QuLogic QuLogic merged commit 1df0529 into matplotlib:master Jul 13, 2021
@jklymak jklymak deleted the fix-twoslopenorm-colorbar branch July 13, 2021 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: color/color & colormaps topic: color/colorbar
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change in custom norm colour map display
3 participants