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