|
16 | 16 | colorbar. |
17 | 17 |
|
18 | 18 | We will start by making a figure of desired size and adding three axes. |
| 19 | +
|
| 20 | +Basic continuous colorbar |
| 21 | +------------------------- |
| 22 | +
|
| 23 | +Set the colormap and norm to correspond to the data for which the colorbar |
| 24 | +will be used. Then create the colorbar by calling |
| 25 | +:class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm |
| 26 | +and orientation as parameters. Here we create a basic continuous colorbar |
| 27 | +with ticks and labels. More information on colorbar api can be found |
| 28 | +`here <https://matplotlib.org/api/colorbar_api.html>`. |
19 | 29 | """ |
20 | 30 |
|
21 | 31 | import matplotlib.pyplot as plt |
22 | 32 | import matplotlib as mpl |
23 | 33 |
|
24 | | -fig, (ax1, ax2, ax3) = plt.subplots(nrows=3) |
25 | | - |
26 | | -############################################################################### |
27 | | -# Basic continuous colorbar |
28 | | -# ------------------------- |
29 | | -# |
30 | | -# Set the colormap and norm to correspond to the data for which the colorbar |
31 | | -# will be used. Then create the colorbar by calling |
32 | | -# :class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm |
33 | | -# and orientation as parameters. Here we create a basic continuous colorbar |
34 | | -# with ticks and labels. More information on colorbar api can be found |
35 | | -# `here <https://matplotlib.org/api/colorbar_api.html>`. |
| 34 | +fig, ax = plt.subplots() |
36 | 35 |
|
37 | 36 | cmap = mpl.cm.cool |
38 | 37 | norm = mpl.colors.Normalize(vmin=5, vmax=10) |
39 | 38 |
|
40 | | -cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, |
| 39 | +cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
41 | 40 | norm=norm, |
42 | 41 | orientation='horizontal') |
43 | 42 | cb1.set_label('Some Units') |
| 43 | +fig.show() |
44 | 44 |
|
45 | 45 | ############################################################################### |
46 | 46 | # Discrete intervals colorbar |
|
64 | 64 | # *extend*, you must specify two extra boundaries. Finally spacing argument |
65 | 65 | # ensures that intervals are shown on colorbar proportionally. |
66 | 66 |
|
| 67 | +fig, ax = plt.subplots() |
| 68 | + |
67 | 69 | cmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan']) |
68 | 70 | cmap.set_over('0.25') |
69 | 71 | cmap.set_under('0.75') |
70 | 72 |
|
71 | 73 | bounds = [1, 2, 4, 7, 8] |
72 | 74 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
73 | | -cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, |
| 75 | +cb2 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
74 | 76 | norm=norm, |
75 | 77 | boundaries=[0] + bounds + [13], |
76 | 78 | extend='both', |
77 | 79 | ticks=bounds, |
78 | 80 | spacing='proportional', |
79 | 81 | orientation='horizontal') |
80 | 82 | cb2.set_label('Discrete intervals, some other units') |
| 83 | +fig.show() |
81 | 84 |
|
82 | 85 | ############################################################################### |
83 | 86 | # Colorbar with custom extension lengths |
|
87 | 90 | # colorbar with discrete intervals. To make the length of each extension same |
88 | 91 | # as the length of the interior colors, use ``extendfrac='auto'``. |
89 | 92 |
|
| 93 | +fig, ax = plt.subplots() |
| 94 | + |
90 | 95 | cmap = mpl.colors.ListedColormap(['royalblue', 'cyan', |
91 | 96 | 'yellow', 'orange']) |
92 | 97 | cmap.set_over('red') |
93 | 98 | cmap.set_under('blue') |
94 | 99 |
|
95 | 100 | bounds = [-1.0, -0.5, 0.0, 0.5, 1.0] |
96 | 101 | norm = mpl.colors.BoundaryNorm(bounds, cmap.N) |
97 | | -cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap, |
| 102 | +cb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap, |
98 | 103 | norm=norm, |
99 | 104 | boundaries=[-10] + bounds + [10], |
100 | 105 | extend='both', |
|
103 | 108 | spacing='uniform', |
104 | 109 | orientation='horizontal') |
105 | 110 | cb3.set_label('Custom extension lengths, some other units') |
106 | | - |
107 | | -plt.tight_layout() |
108 | | -plt.show() |
| 111 | +fig.show() |
0 commit comments