|
3 | 3 | Customized Colorbars Tutorial |
4 | 4 | ============================= |
5 | 5 |
|
6 | | -This tutorial shows how to build colorbars without an attached mappable. |
| 6 | +This tutorial shows how to build colorbars without an attached plot. |
7 | 7 |
|
8 | 8 | """ |
9 | 9 |
|
10 | 10 | ############################################################################### |
11 | 11 | # Customized Colorbars |
12 | 12 | # ==================== |
13 | 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. |
| 14 | +# ColorbarBase derives from ScalarMappable and puts a colorbar in a specified |
| 15 | +# axes, so it has everything needed for a standalone colorbar. It can be used |
| 16 | +# as is to make a colorbar for a given colormap and does not need a mappable |
| 17 | +# object like an image. In this tutorial we will explore what can be done with |
| 18 | +# standalone colorbar. |
19 | 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. |
| 20 | +# We will start by making a figure of desired size and adding thress axes. |
23 | 21 |
|
24 | 22 | import matplotlib.pyplot as plt |
25 | 23 | import matplotlib as mpl |
26 | 24 |
|
27 | | -fig = plt.figure(figsize=(8, 3)) |
28 | | -ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15]) |
29 | | -ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15]) |
30 | | -ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15]) |
| 25 | +fig, (ax1, ax2, ax3) = plt.subplots(nrows=3) |
31 | 26 |
|
32 | 27 | ############################################################################### |
33 | 28 | # Basic continuous colorbar |
|
51 | 46 | # Discrete intervals colorbar |
52 | 47 | # --------------------------- |
53 | 48 | # |
54 | | -# The second example illustrates the use of a ListedColormap which generates |
| 49 | +# The second example illustrates the use of a ListedColormap which generates a |
55 | 50 | # 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 |
| 51 | +# colormap index based on discrete intervals and extended ends to show the |
57 | 52 | # "over" and "under" value colors. Over and under are used to display data |
58 | 53 | # outside of the normalized [0,1] range. Here we pass colors as gray shades as |
59 | 54 | # a string encoding a float in the 0-1 range. |
|
87 | 82 | # Colorbar with custom extension lengths |
88 | 83 | # -------------------------------------- |
89 | 84 | # |
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 |
| 85 | +# Here we illustrate the use of custom length colorbar extensions, used on a |
| 86 | +# colorbar with discrete intervals. Here we pass colors as RGB triplet. To make |
| 87 | +# the length of each extension the same as the length of the interior colors |
| 88 | +# pass the extendfrac argument as auto. |
94 | 89 |
|
95 | 90 | cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], |
96 | 91 | [1., .8, 0.], [1., .4, 0.]]) |
|
109 | 104 | orientation='horizontal') |
110 | 105 | cb3.set_label('Custom extension lengths, some other units') |
111 | 106 |
|
| 107 | +plt.tight_layout() |
112 | 108 | plt.show() |
0 commit comments