Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae9f762

Browse files
committed
DOC: Modernize Colorbar Tick Labelling example
- use colorbar.set_ticks instead of colorbar.ax.set_[x|y]ticklabels whose usage is discouraged - now that we don't have to handle vertical and horizontal colorbars differently we can instead show different ways to achieve the same and show additional tweaks like labelling the colorbar extensions (strictly speaking the old labels >1 and <-1 were simply wrong for clipped data) - update numpy random number generation - add admonition
1 parent f017315 commit ae9f762

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

galleries/examples/ticks/colorbar_tick_labelling_demo.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,63 @@
33
Colorbar Tick Labelling
44
=======================
55
6-
Produce custom labelling for a colorbar.
6+
Different ways to produce custom labelling for a colorbar.
77
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.
912
"""
10-
1113
import matplotlib.pyplot as plt
1214
import numpy as np
13-
from numpy.random import randn
1415

15-
from matplotlib import cm
16+
import matplotlib as mpl
1617

1718
# Fixing random state for reproducibility
18-
np.random.seed(19680801)
19+
rng = np.random.default_rng(seed=19680801)
1920

2021
# %%
2122
# Make plot with vertical (default) colorbar
2223

2324
fig, ax = plt.subplots()
2425

25-
data = np.clip(randn(250, 250), -1, 1)
26+
data = rng.standard_normal((250, 250))
2627

27-
cax = ax.imshow(data, cmap=cm.coolwarm)
28+
cax = ax.imshow(data, vmin=-1, vmax=1, cmap=mpl.cm.coolwarm)
2829
ax.set_title('Gaussian noise with vertical colorbar')
2930

3031
# 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')
3340

3441
# %%
3542
# Make plot with horizontal colorbar
3643

3744
fig, ax = plt.subplots()
3845

39-
data = np.clip(randn(250, 250), -1, 1)
46+
data = np.clip(data, -1, 1)
4047

41-
cax = ax.imshow(data, cmap=cm.afmhot)
48+
cax = ax.imshow(data, cmap=mpl.cm.afmhot)
4249
ax.set_title('Gaussian noise with horizontal colorbar')
4350

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'])
4654

4755
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

Comments
 (0)