|
3 | 3 | Colorbar Tick Labelling
|
4 | 4 | =======================
|
5 | 5 |
|
6 |
| -Produce custom labelling for a colorbar. |
7 |
| -
|
8 |
| -Contributed by Scott Sinclair |
| 6 | +Vertical colorbars have ticks, tick labels, and labels visible on the *y* axis, |
| 7 | +horizontal colorbars on the *x* axis. The ``ticks`` parameter can be used to |
| 8 | +set the ticks and the ``format`` parameter can be used to format the tick labels |
| 9 | +of the visible colorbar axes. For further adjustments, the ``yaxis`` or |
| 10 | +``xaxis`` axes of the colorbar can be retrieved using its ``ax`` property. |
9 | 11 | """
|
10 |
| - |
11 | 12 | import matplotlib.pyplot as plt
|
12 | 13 | import numpy as np
|
13 |
| -from numpy.random import randn |
14 | 14 |
|
15 |
| -from matplotlib import cm |
| 15 | +import matplotlib.ticker as mticker |
16 | 16 |
|
17 | 17 | # Fixing random state for reproducibility
|
18 |
| -np.random.seed(19680801) |
| 18 | +rng = np.random.default_rng(seed=19680801) |
19 | 19 |
|
20 | 20 | # %%
|
21 | 21 | # Make plot with vertical (default) colorbar
|
22 | 22 |
|
23 | 23 | fig, ax = plt.subplots()
|
24 | 24 |
|
25 |
| -data = np.clip(randn(250, 250), -1, 1) |
| 25 | +data = rng.standard_normal((250, 250)) |
26 | 26 |
|
27 |
| -cax = ax.imshow(data, cmap=cm.coolwarm) |
| 27 | +cax = ax.imshow(data, vmin=-1, vmax=1, cmap='coolwarm') |
28 | 28 | ax.set_title('Gaussian noise with vertical colorbar')
|
29 | 29 |
|
30 | 30 | # Add colorbar, make sure to specify tick locations to match desired ticklabels
|
31 |
| -cbar = fig.colorbar(cax, ticks=[-1, 0, 1]) |
32 |
| -cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar |
| 31 | +cbar = fig.colorbar(cax, |
| 32 | + ticks=[-1, 0, 1], |
| 33 | + format=mticker.FixedFormatter(['< -1', '0', '> 1']), |
| 34 | + extend='both' |
| 35 | + ) |
| 36 | +labels = cbar.ax.get_yticklabels() |
| 37 | +labels[0].set_verticalalignment('top') |
| 38 | +labels[-1].set_verticalalignment('bottom') |
33 | 39 |
|
34 | 40 | # %%
|
35 | 41 | # Make plot with horizontal colorbar
|
36 | 42 |
|
37 | 43 | fig, ax = plt.subplots()
|
38 | 44 |
|
39 |
| -data = np.clip(randn(250, 250), -1, 1) |
| 45 | +data = np.clip(data, -1, 1) |
40 | 46 |
|
41 |
| -cax = ax.imshow(data, cmap=cm.afmhot) |
| 47 | +cax = ax.imshow(data, cmap='afmhot') |
42 | 48 | ax.set_title('Gaussian noise with horizontal colorbar')
|
43 | 49 |
|
44 |
| -cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal') |
45 |
| -cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar |
| 50 | +# Add colorbar and adjust ticks afterwards |
| 51 | +cbar = fig.colorbar(cax, orientation='horizontal') |
| 52 | +cbar.set_ticks(ticks=[-1, 0, 1], labels=['Low', 'Medium', 'High']) |
46 | 53 |
|
47 | 54 | plt.show()
|
| 55 | + |
| 56 | +# %% |
| 57 | +# |
| 58 | +# .. admonition:: References |
| 59 | +# |
| 60 | +# The use of the following functions, methods, classes and modules is shown |
| 61 | +# in this example: |
| 62 | +# |
| 63 | +# - `matplotlib.colorbar.Colorbar.set_ticks` |
| 64 | +# - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar` |
0 commit comments