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