|
| 1 | +''' |
| 2 | +==================== |
| 3 | +Customized colorbars |
| 4 | +==================== |
| 5 | +
|
| 6 | +This example shows how to build colorbars without an attached mappable. |
| 7 | +''' |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import matplotlib as mpl |
| 11 | + |
| 12 | +# Make a figure and axes with dimensions as desired. |
| 13 | +fig = plt.figure(figsize=(8, 5)) |
| 14 | +ax1 = fig.add_axes([0.05, 0.85, 0.9, 0.1]) |
| 15 | +ax2 = fig.add_axes([0.05, 0.6, 0.9, 0.1]) |
| 16 | +ax3 = fig.add_axes([0.05, 0.35, 0.9, 0.1]) |
| 17 | +ax4 = fig.add_axes([0.05, 0.1, 0.9, 0.1]) |
| 18 | + |
| 19 | +# Set the colormap and norm to correspond to the data for which |
| 20 | +# the colorbar will be used. |
| 21 | +cmap = mpl.cm.cool |
| 22 | +norm = mpl.colors.Normalize(vmin=5, vmax=10) |
| 23 | + |
| 24 | +# ColorbarBase derives from ScalarMappable and puts a colorbar |
| 25 | +# in a specified axes, so it has everything needed for a |
| 26 | +# standalone colorbar. There are many more kwargs, but the |
| 27 | +# following gives a basic continuous colorbar with ticks |
| 28 | +# and labels. |
| 29 | +cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, |
| 30 | + norm=norm, |
| 31 | + orientation='horizontal') |
| 32 | +cb1.set_label('Some Units') |
| 33 | + |
| 34 | +# The second example shows how to make a discrete colorbar based on a |
| 35 | +# continuous cmapnorm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both') |
| 36 | +cmap = mpl.cm.viridis |
| 37 | +bounds = [-1, 2, 5, 7, 12, 15] |
| 38 | +norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both') |
| 39 | +cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, |
| 40 | + norm=norm, |
| 41 | + orientation='horizontal') |
| 42 | +cb2.set_label("Discrete intervals with extend='both' keyword") |
| 43 | + |
| 44 | + |
| 45 | +# The third example illustrates the use of a ListedColormap, a |
| 46 | +# BoundaryNorm, and extended ends to show the "over" and "under" |
| 47 | +# value colors. |
| 48 | +cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c']) |
| 49 | +cmap.set_over('0.25') |
| 50 | +cmap.set_under('0.75') |
| 51 | + |
| 52 | +# If a ListedColormap is used, the length of the bounds array must be |
| 53 | +# one greater than the length of the color list. The bounds must be |
| 54 | +# monotonically increasing. |
| 55 | +bounds = [1, 2, 4, 7, 8] |
| 56 | +norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
| 57 | +cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap, |
| 58 | + norm=norm, |
| 59 | + # to use 'extend', you must |
| 60 | + # specify two extra boundaries: |
| 61 | + boundaries=[0] + bounds + [13], |
| 62 | + extend='both', |
| 63 | + ticks=bounds, # optional |
| 64 | + spacing='proportional', |
| 65 | + orientation='horizontal') |
| 66 | +cb3.set_label('Discrete intervals, some other units') |
| 67 | + |
| 68 | +# The fourth example illustrates the use of custom length colorbar |
| 69 | +# extensions, used on a colorbar with discrete intervals. |
| 70 | +cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], |
| 71 | + [1., .8, 0.], [1., .4, 0.]]) |
| 72 | +cmap.set_over((1., 0., 0.)) |
| 73 | +cmap.set_under((0., 0., 1.)) |
| 74 | + |
| 75 | +bounds = [-1., -.5, 0., .5, 1.] |
| 76 | +norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
| 77 | +cb4 = mpl.colorbar.ColorbarBase(ax4, cmap=cmap, |
| 78 | + norm=norm, |
| 79 | + boundaries=[-10] + bounds + [10], |
| 80 | + extend='both', |
| 81 | + # Make the length of each extension |
| 82 | + # the same as the length of the |
| 83 | + # interior colors: |
| 84 | + extendfrac='auto', |
| 85 | + ticks=bounds, |
| 86 | + spacing='uniform', |
| 87 | + orientation='horizontal') |
| 88 | +cb4.set_label('Custom extension lengths, some other units') |
| 89 | + |
| 90 | +plt.show() |
0 commit comments