Skip to content

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

Closed
@fmaussion

Description

@fmaussion

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions