|
1 |
| -''' |
2 |
| -==================== |
3 |
| -Customized colorbars |
4 |
| -==================== |
| 1 | +""" |
| 2 | +============================= |
| 3 | +Customized Colorbars Tutorial |
| 4 | +============================= |
5 | 5 |
|
6 |
| -This example shows how to build colorbars without an attached mappable. |
7 |
| -''' |
| 6 | +This tutorial shows how to build colorbars without an attached mappable. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +############################################################################### |
| 11 | +# Customized Colorbars |
| 12 | +# ==================== |
| 13 | +# |
| 14 | +# `matplotlib.colorbar.ColorbarBase` derives from `ScalarMappable` and puts a |
| 15 | +# colorbar in specified axes, it is the base class with standalone colorbar |
| 16 | +# drawing functionality. It can be used as-is to make a colorbar for a given |
| 17 | +# colormap and does not need a mappable object like an image. In this tutorial |
| 18 | +# we will explore what can be done with standalone colorbar. |
| 19 | +# |
| 20 | +# We will start by making a figure of desired size and adding axis at position |
| 21 | +# [left, bottom, width, height] where all quantities are in fractions of figure |
| 22 | +# width and height. |
8 | 23 |
|
9 | 24 | import matplotlib.pyplot as plt
|
10 | 25 | import matplotlib as mpl
|
11 | 26 |
|
12 |
| -# Make a figure and axes with dimensions as desired. |
13 | 27 | fig = plt.figure(figsize=(8, 3))
|
14 | 28 | ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
|
15 | 29 | ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
|
16 | 30 | ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15])
|
17 | 31 |
|
18 |
| -# Set the colormap and norm to correspond to the data for which |
19 |
| -# the colorbar will be used. |
| 32 | +############################################################################### |
| 33 | +# Basic continuous colorbar |
| 34 | +# ------------------------- |
| 35 | +# |
| 36 | +# Set the colormap and norm to correspond to the data for which the colorbar |
| 37 | +# will be used. Then create the colorbar by calling `ColorbarBase` and |
| 38 | +# specify axis, colormap, norm and orientation as parameters. Here we create |
| 39 | +# a basic continuous colorbar with ticks and labels. There are many more kwargs |
| 40 | +# which can be used to further modify the colorbar. |
| 41 | + |
20 | 42 | cmap = mpl.cm.cool
|
21 | 43 | norm = mpl.colors.Normalize(vmin=5, vmax=10)
|
22 | 44 |
|
23 |
| -# ColorbarBase derives from ScalarMappable and puts a colorbar |
24 |
| -# in a specified axes, so it has everything needed for a |
25 |
| -# standalone colorbar. There are many more kwargs, but the |
26 |
| -# following gives a basic continuous colorbar with ticks |
27 |
| -# and labels. |
28 | 45 | cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
|
29 | 46 | norm=norm,
|
30 | 47 | orientation='horizontal')
|
31 | 48 | cb1.set_label('Some Units')
|
32 | 49 |
|
33 |
| -# The second example illustrates the use of a ListedColormap, a |
34 |
| -# BoundaryNorm, and extended ends to show the "over" and "under" |
35 |
| -# value colors. |
| 50 | +############################################################################### |
| 51 | +# Discrete intervals colorbar |
| 52 | +# --------------------------- |
| 53 | +# |
| 54 | +# The second example illustrates the use of a ListedColormap which generates |
| 55 | +# colormap from a set of listed colors, a BoundaryNorm which generates a |
| 56 | +# colormap index based on discrete interval and extended ends to show the |
| 57 | +# "over" and "under" value colors. Over and under are used to display data |
| 58 | +# outside of the normalized [0,1] range. Here we pass colors as gray shades as |
| 59 | +# a string encoding a float in the 0-1 range. |
| 60 | +# |
| 61 | +# If a ListedColormap is used, the length of the bounds array must be |
| 62 | +# one greater than the length of the color list. The bounds must be |
| 63 | +# monotonically increasing. |
| 64 | +# |
| 65 | +# This time we pass some more arguments in addition to previous arguments to |
| 66 | +# ColorBase. For the out-of-range values to display on the colorbar, we have to |
| 67 | +# use the extend keyword argument. To use 'extend', you must specify two extra |
| 68 | +# boundaries. Finally spacing argument ensures that intervals are shown on |
| 69 | +# colorbar proportionally. |
| 70 | + |
36 | 71 | cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
|
37 | 72 | cmap.set_over('0.25')
|
38 | 73 | cmap.set_under('0.75')
|
39 | 74 |
|
40 |
| -# If a ListedColormap is used, the length of the bounds array must be |
41 |
| -# one greater than the length of the color list. The bounds must be |
42 |
| -# monotonically increasing. |
43 | 75 | bounds = [1, 2, 4, 7, 8]
|
44 | 76 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
|
45 | 77 | cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
|
46 | 78 | norm=norm,
|
47 |
| - # to use 'extend', you must |
48 |
| - # specify two extra boundaries: |
49 | 79 | boundaries=[0] + bounds + [13],
|
50 | 80 | extend='both',
|
51 |
| - ticks=bounds, # optional |
| 81 | + ticks=bounds, |
52 | 82 | spacing='proportional',
|
53 | 83 | orientation='horizontal')
|
54 | 84 | cb2.set_label('Discrete intervals, some other units')
|
55 | 85 |
|
56 |
| -# The third example illustrates the use of custom length colorbar |
57 |
| -# extensions, used on a colorbar with discrete intervals. |
| 86 | +############################################################################### |
| 87 | +# Colorbar with custom extension lengths |
| 88 | +# -------------------------------------- |
| 89 | +# |
| 90 | +# Now in the third example we illustrate the use of custom length colorbar |
| 91 | +# extensions, used on a colorbar with discrete intervals. Here we pass colors |
| 92 | +# as RGB triplet. To make the length of each extension the same as the length |
| 93 | +# of the interior colors pass extendfrac argument as auto |
| 94 | + |
58 | 95 | cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.],
|
59 | 96 | [1., .8, 0.], [1., .4, 0.]])
|
60 | 97 | cmap.set_over((1., 0., 0.))
|
|
66 | 103 | norm=norm,
|
67 | 104 | boundaries=[-10] + bounds + [10],
|
68 | 105 | extend='both',
|
69 |
| - # Make the length of each extension |
70 |
| - # the same as the length of the |
71 |
| - # interior colors: |
72 | 106 | extendfrac='auto',
|
73 | 107 | ticks=bounds,
|
74 | 108 | spacing='uniform',
|
75 | 109 | orientation='horizontal')
|
76 | 110 | cb3.set_label('Custom extension lengths, some other units')
|
77 | 111 |
|
78 | 112 | plt.show()
|
| 113 | + |
0 commit comments