|
8 | 8 | This tutorial shows how to build and customize standalone colorbars, i.e. |
9 | 9 | without an attached plot. |
10 | 10 |
|
11 | | -A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`) |
12 | | -object (typically, an image) which indicates the colormap and the norm to be |
13 | | -used. In order to create a colorbar without an attached image, one can instead |
14 | | -use a `.ScalarMappable` with no associated data. |
| 11 | +A `~.Figure.colorbar` needs a "mappable" (`matplotlib.colorizer.ColorizingArtist`) |
| 12 | +object (typically, an image) which contains a colorizer |
| 13 | +(`matplotlib.colorizer.Colorizer`) that holds the data-to-color pipeline (norm and |
| 14 | +colormap). In order to create a colorbar without an attached image, one can instead |
| 15 | +use a `.ColorizingArtist` with no associated data. |
15 | 16 | """ |
16 | 17 |
|
17 | 18 | import matplotlib.pyplot as plt |
|
23 | 24 | # ------------------------- |
24 | 25 | # Here, we create a basic continuous colorbar with ticks and labels. |
25 | 26 | # |
26 | | -# The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable` |
27 | | -# (constructed using the *norm* and *cmap* arguments), the axes where the |
28 | | -# colorbar should be drawn, and the colorbar's orientation. |
| 27 | +# The arguments to the `~.Figure.colorbar` call are a `.ColorizingArtist`, |
| 28 | +# the axes where the colorbar should be drawn, and the colorbar's orientation. |
| 29 | +# To crate a `.ColorizingArtist` one must first make `.Colorizer` that holds the |
| 30 | +# desired *norm* and *cmap*. |
| 31 | +# |
29 | 32 | # |
30 | 33 | # For more information see the `~matplotlib.colorbar` API. |
31 | 34 |
|
32 | 35 | fig, ax = plt.subplots(figsize=(6, 1), layout='constrained') |
33 | 36 |
|
34 | 37 | norm = mpl.colors.Normalize(vmin=5, vmax=10) |
35 | 38 |
|
36 | | -fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap="cool"), |
| 39 | +colorizer = mpl.colorizer.Colorizer(norm=norm, cmap="cool") |
| 40 | + |
| 41 | +fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer), |
37 | 42 | cax=ax, orientation='horizontal', label='Some Units') |
38 | 43 |
|
39 | 44 | # %% |
|
47 | 52 |
|
48 | 53 | fig, ax = plt.subplots(layout='constrained') |
49 | 54 |
|
50 | | -fig.colorbar(mpl.cm.ScalarMappable(norm=mpl.colors.Normalize(0, 1), cmap='magma'), |
| 55 | +colorizer = mpl.colorizer.Colorizer(norm=mpl.colors.Normalize(0, 1), cmap='magma') |
| 56 | + |
| 57 | +fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer), |
51 | 58 | ax=ax, orientation='vertical', label='a colorbar label') |
52 | 59 |
|
53 | 60 | # %% |
|
65 | 72 | bounds = [-1, 2, 5, 7, 12, 15] |
66 | 73 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both') |
67 | 74 |
|
68 | | -fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap="viridis"), |
| 75 | +colorizer = mpl.colorizer.Colorizer(norm=norm, cmap='viridis') |
| 76 | + |
| 77 | +fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer), |
69 | 78 | cax=ax, orientation='horizontal', |
70 | 79 | label="Discrete intervals with extend='both' keyword") |
71 | 80 |
|
|
94 | 103 | bounds = [1, 2, 4, 7, 8] |
95 | 104 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
96 | 105 |
|
| 106 | +colorizer = mpl.colorizer.Colorizer(norm=norm, cmap=cmap) |
| 107 | + |
97 | 108 | fig.colorbar( |
98 | | - mpl.cm.ScalarMappable(cmap=cmap, norm=norm), |
| 109 | + mpl.colorizer.ColorizingArtist(colorizer), |
99 | 110 | cax=ax, orientation='horizontal', |
100 | 111 | extend='both', |
101 | 112 | spacing='proportional', |
|
116 | 127 | bounds = [-1.0, -0.5, 0.0, 0.5, 1.0] |
117 | 128 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
118 | 129 |
|
| 130 | +colorizer = mpl.colorizer.Colorizer(norm=norm, cmap=cmap) |
| 131 | + |
119 | 132 | fig.colorbar( |
120 | | - mpl.cm.ScalarMappable(cmap=cmap, norm=norm), |
| 133 | + mpl.colorizer.ColorizingArtist(colorizer), |
121 | 134 | cax=ax, orientation='horizontal', |
122 | 135 | extend='both', extendfrac='auto', |
123 | 136 | spacing='uniform', |
|
0 commit comments