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

Skip to content

Commit 1542cee

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 1542cee

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

galleries/examples/ticks/colorbar_tick_labelling_demo.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,62 @@
33
Colorbar Tick Labelling
44
=======================
55
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.
911
"""
10-
1112
import matplotlib.pyplot as plt
1213
import numpy as np
13-
from numpy.random import randn
1414

15-
from matplotlib import cm
15+
import matplotlib.ticker as mticker
1616

1717
# Fixing random state for reproducibility
18-
np.random.seed(19680801)
18+
rng = np.random.default_rng(seed=19680801)
1919

2020
# %%
2121
# Make plot with vertical (default) colorbar
2222

2323
fig, ax = plt.subplots()
2424

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

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

3030
# 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')
3339

3440
# %%
3541
# Make plot with horizontal colorbar
3642

3743
fig, ax = plt.subplots()
3844

39-
data = np.clip(randn(250, 250), -1, 1)
45+
data = np.clip(data, -1, 1)
4046

41-
cax = ax.imshow(data, cmap=cm.afmhot)
47+
cax = ax.imshow(data, cmap='afmhot')
4248
ax.set_title('Gaussian noise with horizontal colorbar')
4349

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

4754
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

Comments
 (0)