Skip to content

Add an "extend" keyword to colors.BoundaryNorm #4850

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

Closed
fmaussion opened this issue Aug 1, 2015 · 4 comments
Closed

Add an "extend" keyword to colors.BoundaryNorm #4850

fmaussion opened this issue Aug 1, 2015 · 4 comments
Milestone

Comments

@fmaussion
Copy link
Contributor

I've always found the third colorbar example from the gallery quite inelegant (http://matplotlib.org/examples/api/colorbar_only.html). Very often, you would like to extend your colorbar while using the same color scheme as the current colormap, not by adding new colors with set_over and set_under.

Maybe I am missing something, but the current behaviour without set_over leads to the same color for the last box of the colorbar and the extension. I found it quite easy to add a new extend keyword to the existing BoundaryNorm class that does the exact same thing as before but handles the color choice differently. It is best shown with the example below:

import matplotlib as mpl
from matplotlib import pyplot
fig = pyplot.figure(figsize=(8,3))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15])
bounds = [-1, 2, 5, 7, 12, 15]
cmap = mpl.cm.get_cmap('jet')

# Default behavior
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
                                     norm=norm,
                                     extend='both',
                                     orientation='horizontal')
cb1.set_label('Default BoundaryNorm ouput');

# With the new keyword
norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
                                     norm=norm,
                                     # Ideally this keyword could 
                                     # be omitted since it is redundant
                                     extend='both',
                                     orientation='horizontal')
cb2.set_label('With the new keyword');

# Just max
norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='max')
cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap,
                                     norm=norm,
                                     extend='max',
                                     orientation='horizontal')
cb3.set_label('Just max');

Which produces:
colorbar_demo

What do you think? If accepted I could easily add it to my ongoing PR

@efiring
Copy link
Member

efiring commented Aug 1, 2015

It should be a separate PR. I see the logic of what you want, but we need to be careful about how this relates to other uses of "extend" (colorbar and contourf) and to other norms.

@fmaussion
Copy link
Contributor Author

Sounds reasonable: I'll wait for the current PR to be closed first.

@fmaussion
Copy link
Contributor Author

The new related PR: #5034

@rcomer
Copy link
Member

rcomer commented Dec 10, 2022

Looks like this was done at #17534.

@rcomer rcomer closed this as completed Dec 10, 2022
@QuLogic QuLogic added this to the v3.3.0 milestone Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants