|
| 1 | +New "extend" keyword to colors.BoundaryNorm |
| 2 | +------------------------------------------- |
| 3 | + |
| 4 | +:func:`~matplotlib.colors.BoundaryNorm` now has an ``extend`` kwarg. This is |
| 5 | +useful when creating a discrete colorbar from a continuous colormap: when |
| 6 | +setting ``extend`` to ``'both'``, ``'min'`` or ``'max'``, the colors are |
| 7 | +interpolated so that the extensions have a different color than the inner |
| 8 | +cells. |
| 9 | + |
| 10 | +Example |
| 11 | +``````` |
| 12 | +:: |
| 13 | + |
| 14 | + from matplotlib import pyplot as plt |
| 15 | + import matplotlib as mpl |
| 16 | + |
| 17 | + # Make a figure and axes with dimensions as desired. |
| 18 | + fig = plt.figure(figsize=(8, 3)) |
| 19 | + ax1 = fig.add_axes([0.05, 0.7, 0.9, 0.2]) |
| 20 | + ax2 = fig.add_axes([0.05, 0.2, 0.9, 0.2]) |
| 21 | + |
| 22 | + # Set the colormap and bounds |
| 23 | + bounds = [-1, 2, 5, 7, 12, 15] |
| 24 | + cmap = mpl.cm.get_cmap('viridis') |
| 25 | + |
| 26 | + # Default behavior |
| 27 | + norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
| 28 | + cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, |
| 29 | + norm=norm, |
| 30 | + extend='both', |
| 31 | + orientation='horizontal') |
| 32 | + cb1.set_label('Default BoundaryNorm ouput') |
| 33 | + |
| 34 | + # New behavior |
| 35 | + norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both') |
| 36 | + cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, |
| 37 | + norm=norm, |
| 38 | + orientation='horizontal') |
| 39 | + cb2.set_label("With new extend='both' keyword") |
| 40 | + |
| 41 | + plt.show() |
0 commit comments