|
3 | 3 | Customized Colorbars Tutorial
|
4 | 4 | =============================
|
5 | 5 |
|
6 |
| -This tutorial shows how to build colorbars without an attached plot. |
| 6 | +This tutorial shows how to build and customize standalone colorbars, i.e. |
| 7 | +without an attached plot. |
7 | 8 |
|
8 | 9 | Customized Colorbars
|
9 | 10 | ====================
|
10 | 11 |
|
11 |
| -`~matplotlib.colorbar.ColorbarBase` puts a colorbar in a specified axes, |
12 |
| -and can make a colorbar for a given colormap; it does not need a mappable |
13 |
| -object like an image. In this tutorial we will explore what can be done with |
14 |
| -standalone colorbar. |
| 12 | +A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`) |
| 13 | +object (typically, an image) which indicates the colormap and the norm to be |
| 14 | +used. In order to create a colorbar without an attached image, one can instead |
| 15 | +use a `.ScalarMappable` with no associated data. |
15 | 16 |
|
16 | 17 | Basic continuous colorbar
|
17 | 18 | -------------------------
|
18 | 19 |
|
19 |
| -Set the colormap and norm to correspond to the data for which the colorbar |
20 |
| -will be used. Then create the colorbar by calling |
21 |
| -:class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm |
22 |
| -and orientation as parameters. Here we create a basic continuous colorbar |
23 |
| -with ticks and labels. For more information see the |
24 |
| -:mod:`~matplotlib.colorbar` API. |
| 20 | +Here we create a basic continuous colorbar with ticks and labels. |
| 21 | +
|
| 22 | +The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable` |
| 23 | +(constructed using the *norm* and *cmap* arguments), the axes where the |
| 24 | +colorbar should be drawn, and the colorbar's orientation. |
| 25 | +
|
| 26 | +For more information see the :mod:`~matplotlib.colorbar` API. |
25 | 27 | """
|
26 | 28 |
|
27 | 29 | import matplotlib.pyplot as plt
|
|
33 | 35 | cmap = mpl.cm.cool
|
34 | 36 | norm = mpl.colors.Normalize(vmin=5, vmax=10)
|
35 | 37 |
|
36 |
| -cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
37 |
| - norm=norm, |
38 |
| - orientation='horizontal') |
39 |
| -cb1.set_label('Some Units') |
40 |
| -fig.show() |
| 38 | +fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap), |
| 39 | + cax=ax, orientation='horizontal', label='Some Units') |
41 | 40 |
|
42 | 41 | ###############################################################################
|
43 | 42 | # Discrete intervals colorbar
|
|
56 | 55 | # bounds must be monotonically increasing.
|
57 | 56 | #
|
58 | 57 | # This time we pass some more arguments in addition to previous arguments to
|
59 |
| -# :class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to |
| 58 | +# `~.Figure.colorbar`. For the out-of-range values to |
60 | 59 | # display on the colorbar, we have to use the *extend* keyword argument. To use
|
61 | 60 | # *extend*, you must specify two extra boundaries. Finally spacing argument
|
62 | 61 | # ensures that intervals are shown on colorbar proportionally.
|
|
70 | 69 |
|
71 | 70 | bounds = [1, 2, 4, 7, 8]
|
72 | 71 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
|
73 |
| -cb2 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
74 |
| - norm=norm, |
75 |
| - boundaries=[0] + bounds + [13], |
76 |
| - extend='both', |
77 |
| - ticks=bounds, |
78 |
| - spacing='proportional', |
79 |
| - orientation='horizontal') |
80 |
| -cb2.set_label('Discrete intervals, some other units') |
81 |
| -fig.show() |
| 72 | +fig.colorbar( |
| 73 | + mpl.cm.ScalarMappable(cmap=cmap, norm=norm), |
| 74 | + cax=ax, |
| 75 | + boundaries=[0] + bounds + [13], |
| 76 | + extend='both', |
| 77 | + ticks=bounds, |
| 78 | + spacing='proportional', |
| 79 | + orientation='horizontal', |
| 80 | + label='Discrete intervals, some other units', |
| 81 | +) |
82 | 82 |
|
83 | 83 | ###############################################################################
|
84 | 84 | # Colorbar with custom extension lengths
|
|
98 | 98 |
|
99 | 99 | bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
|
100 | 100 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
|
101 |
| -cb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
102 |
| - norm=norm, |
103 |
| - boundaries=[-10] + bounds + [10], |
104 |
| - extend='both', |
105 |
| - extendfrac='auto', |
106 |
| - ticks=bounds, |
107 |
| - spacing='uniform', |
108 |
| - orientation='horizontal') |
109 |
| -cb3.set_label('Custom extension lengths, some other units') |
110 |
| -fig.show() |
| 101 | +fig.colorbar( |
| 102 | + mpl.cm.ScalarMappable(cmap=cmap, norm=norm), |
| 103 | + cax=ax, |
| 104 | + boundaries=[-10] + bounds + [10], |
| 105 | + extend='both', |
| 106 | + extendfrac='auto', |
| 107 | + ticks=bounds, |
| 108 | + spacing='uniform', |
| 109 | + orientation='horizontal', |
| 110 | + label='Custom extension lengths, some other units', |
| 111 | +) |
| 112 | + |
| 113 | +plt.show() |
0 commit comments