|
1 | 1 | """ |
2 | 2 | .. redirect-from:: /tutorials/colors/colorbar_only |
3 | 3 |
|
4 | | -============================= |
5 | | -Customized Colorbars Tutorial |
6 | | -============================= |
| 4 | +==================== |
| 5 | +Standalone colorbars |
| 6 | +==================== |
7 | 7 |
|
8 | 8 | This tutorial shows how to build and customize standalone colorbars, i.e. |
9 | 9 | without an attached plot. |
10 | 10 |
|
11 | | -Customized Colorbars |
12 | | -==================== |
13 | | -
|
14 | 11 | A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`) |
15 | 12 | object (typically, an image) which indicates the colormap and the norm to be |
16 | 13 | used. In order to create a colorbar without an attached image, one can instead |
17 | 14 | use a `.ScalarMappable` with no associated data. |
18 | | -
|
19 | | -Basic continuous colorbar |
20 | | -------------------------- |
21 | | -
|
22 | | -Here we create a basic continuous colorbar with ticks and labels. |
23 | | -
|
24 | | -The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable` |
25 | | -(constructed using the *norm* and *cmap* arguments), the axes where the |
26 | | -colorbar should be drawn, and the colorbar's orientation. |
27 | | -
|
28 | | -For more information see the :mod:`~matplotlib.colorbar` API. |
29 | 15 | """ |
30 | 16 |
|
31 | 17 | importmatplotlib.pyplotasplt |
32 | | - |
33 | 18 | importmatplotlibasmpl |
34 | 19 |
|
35 | | -fig,ax=plt.subplots(figsize=(6,1)) |
36 | | -fig.subplots_adjust(bottom=0.5) |
| 20 | +# %% |
| 21 | +# Basic continuous colorbar |
| 22 | +# ------------------------- |
| 23 | +# Here, we create a basic continuous colorbar with ticks and labels. |
| 24 | +# |
| 25 | +# The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable` |
| 26 | +# (constructed using the *norm* and *cmap* arguments), the axes where the |
| 27 | +# colorbar should be drawn, and the colorbar's orientation. |
| 28 | +# |
| 29 | +# For more information see the `~matplotlib.colorbar` API. |
| 30 | + |
| 31 | +fig,ax=plt.subplots(figsize=(6,1),layout='constrained') |
37 | 32 |
|
38 | 33 | cmap=mpl.cm.cool |
39 | 34 | norm=mpl.colors.Normalize(vmin=5,vmax=10) |
40 | 35 |
|
41 | 36 | fig.colorbar(mpl.cm.ScalarMappable(norm=norm,cmap=cmap), |
42 | 37 | cax=ax,orientation='horizontal',label='Some Units') |
43 | 38 |
|
| 39 | +# %% |
| 40 | +# Colorbar attached next to a pre-existing axes |
| 41 | +# --------------------------------------------- |
| 42 | +# All examples in this tutorial (except this one) show a standalone colorbar on |
| 43 | +# its own figure, but it is possible to display the colorbar *next* to a |
| 44 | +# pre-existing Axes *ax* by passing ``ax=ax`` to the colorbar() call (meaning |
| 45 | +# "draw the colorbar next to *ax*") rather than ``cax=ax`` (meaning "draw the |
| 46 | +# colorbar on *ax*"). |
| 47 | + |
| 48 | +fig,ax=plt.subplots(layout='constrained') |
| 49 | + |
| 50 | +fig.colorbar(mpl.cm.ScalarMappable(norm=mpl.colors.Normalize(0,1),cmap='magma'), |
| 51 | +ax=ax,orientation='vertical',label='a colorbar label') |
44 | 52 |
|
45 | 53 | # %% |
46 | | -# Extended colorbar with continuous colorscale |
47 | | -# -------------------------------------------- |
48 | | -# |
49 | | -# The second example shows how to make a discrete colorbar based on a |
50 | | -# continuous cmap. With the "extend" keyword argument the appropriate colors |
51 | | -# are chosen to fill the colorspace, including the extensions: |
52 | | -fig,ax=plt.subplots(figsize=(6,1)) |
53 | | -fig.subplots_adjust(bottom=0.5) |
| 54 | +# Discrete and extended colorbar with continuous colorscale |
| 55 | +# --------------------------------------------------------- |
| 56 | +# The following example shows how to make a discrete colorbar based on a |
| 57 | +# continuous cmap. We use `matplotlib.colors.BoundaryNorm` to describe the |
| 58 | +# interval boundaries (which must be in increasing order), and further pass the |
| 59 | +# *extend* argument to it to further display "over" and "under" colors (which |
| 60 | +# are used for data outside of the norm range). |
| 61 | + |
| 62 | +fig,ax=plt.subplots(figsize=(6,1),layout='constrained') |
54 | 63 |
|
55 | 64 | cmap=mpl.cm.viridis |
56 | 65 | bounds= [-1,2,5,7,12,15] |
|
61 | 70 | label="Discrete intervals with extend='both' keyword") |
62 | 71 |
|
63 | 72 | # %% |
64 | | -# Discrete intervals colorbar |
65 | | -# --------------------------- |
66 | | -# |
67 | | -# The third example illustrates the use of a |
68 | | -# :class:`~matplotlib.colors.ListedColormap` which generates a colormap from a |
69 | | -# set of listed colors, `.colors.BoundaryNorm` which generates a colormap |
70 | | -# index based on discrete intervals and extended ends to show the "over" and |
71 | | -# "under" value colors. Over and under are used to display data outside of the |
72 | | -# normalized [0, 1] range. Here we pass colors as gray shades as a string |
73 | | -# encoding a float in the 0-1 range. |
| 73 | +# Colorbar with arbitrary colors |
| 74 | +# ------------------------------ |
| 75 | +# The following example still uses a `.BoundaryNorm` to describe discrete |
| 76 | +# interval boundaries, but now uses a `matplotlib.colors.ListedColormap` to |
| 77 | +# associate each interval with an arbitrary color (there must be as many |
| 78 | +# intervals than there are colors). The "over" and "under" colors are set on |
| 79 | +# the colormap using `.Colormap.with_extremes`. |
74 | 80 | # |
75 | | -# If a :class:`~matplotlib.colors.ListedColormap` is used, the length of the |
76 | | -# bounds array must be one greater than the length of the color list. The |
77 | | -# bounds must be monotonically increasing. |
| 81 | +# We also pass additional arguments to `~.Figure.colorbar`: |
78 | 82 | # |
79 | | -# This time we pass additional arguments to |
80 | | -# `~.Figure.colorbar`. For the out-of-range values to display on the colorbar |
81 | | -# without using the *extend* keyword with |
82 | | -# `.colors.BoundaryNorm`, we have to use the *extend* keyword argument directly |
83 | | -# in the colorbar call. Here we also |
84 | | -# use the spacing argument to make |
85 | | -# the length of each colorbar segment proportional to its corresponding |
86 | | -# interval. |
87 | | - |
88 | | -fig,ax=plt.subplots(figsize=(6,1)) |
89 | | -fig.subplots_adjust(bottom=0.5) |
| 83 | +# - To display the out-of-range values on the colorbar, we use the *extend* |
| 84 | +# argument in the colorbar() call. (This is equivalent to passing the |
| 85 | +# *extend* argument in the `.BoundaryNorm` constructor as done in the |
| 86 | +# previous example.) |
| 87 | +# - To make the length of each colorbar segment proportional to its |
| 88 | +# corresponding interval, we use the *spacing* argument in the colorbar() |
| 89 | +# call. |
90 | 90 |
|
91 | | -cmap= (mpl.colors.ListedColormap(['red','green','blue','cyan']) |
92 | | - .with_extremes(over='0.25',under='0.75')) |
| 91 | +fig,ax=plt.subplots(figsize=(6,1),layout='constrained') |
93 | 92 |
|
| 93 | +cmap= (mpl.colors.ListedColormap(['red','green','blue','cyan']) |
| 94 | + .with_extremes(under='yellow',over='magenta')) |
94 | 95 | bounds= [1,2,4,7,8] |
95 | 96 | norm=mpl.colors.BoundaryNorm(bounds,cmap.N) |
| 97 | + |
96 | 98 | fig.colorbar( |
97 | 99 | mpl.cm.ScalarMappable(cmap=cmap,norm=norm), |
98 | | -cax=ax, |
| 100 | +cax=ax,orientation='horizontal', |
99 | 101 | extend='both', |
100 | | -ticks=bounds, |
101 | 102 | spacing='proportional', |
102 | | -orientation='horizontal', |
103 | 103 | label='Discrete intervals, some other units', |
104 | 104 | ) |
105 | 105 |
|
106 | 106 | # %% |
107 | 107 | # Colorbar with custom extension lengths |
108 | 108 | # -------------------------------------- |
109 | | -# |
110 | | -# Here we illustrate the use of custom length colorbar extensions, on a |
111 | | -# colorbar with discrete intervals. To make the length of each extension the |
| 109 | +# We can customize the length colorbar extensions, on a colorbar with discrete |
| 110 | +# intervals. To make the length of each extension the |
112 | 111 | # same as the length of the interior colors, use ``extendfrac='auto'``. |
113 | 112 |
|
114 | | -fig,ax=plt.subplots(figsize=(6,1)) |
115 | | -fig.subplots_adjust(bottom=0.5) |
| 113 | +fig,ax=plt.subplots(figsize=(6,1),layout='constrained') |
116 | 114 |
|
117 | 115 | cmap= (mpl.colors.ListedColormap(['royalblue','cyan','yellow','orange']) |
118 | 116 | .with_extremes(over='red',under='blue')) |
119 | | - |
120 | 117 | bounds= [-1.0,-0.5,0.0,0.5,1.0] |
121 | 118 | norm=mpl.colors.BoundaryNorm(bounds,cmap.N) |
| 119 | + |
122 | 120 | fig.colorbar( |
123 | 121 | mpl.cm.ScalarMappable(cmap=cmap,norm=norm), |
124 | | -cax=ax, |
125 | | -extend='both', |
126 | | -extendfrac='auto', |
127 | | -ticks=bounds, |
| 122 | +cax=ax,orientation='horizontal', |
| 123 | +extend='both',extendfrac='auto', |
128 | 124 | spacing='uniform', |
129 | | -orientation='horizontal', |
130 | 125 | label='Custom extension lengths, some other units', |
131 | 126 | ) |
132 | 127 |
|
|