Skip to content

Commit 5f1e34c

Browse files
story645rutj3jklymaktimhoffmtacaswell
committed
added a reversed section to colormap reference and reversing section to
colormap tutorial Co-authored-by: RutgerK <2157033+RutgerK@users.noreply.github.com> Co-authored-by: Jody Klymak <jklymak@gmail.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
1 parent b637f41 commit 5f1e34c

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

examples/color/colormap_reference.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
Reference for colormaps included with Matplotlib.
77
88
A reversed version of each of these colormaps is available by appending
9-
``_r`` to the name, e.g., ``viridis_r``.
9+
``_r`` to the name, as shown in :ref:`reverse-cmap`.
1010
1111
See :doc:`/tutorials/colors/colormaps` for an in-depth discussion about
12-
colormaps, including colorblind-friendliness.
12+
colormaps, including colorblind-friendliness, and
13+
:doc:`/tutorials/colors/colormap-manipulation` for a guide to creating
14+
colormaps.
1315
"""
1416

1517
import numpy as np
1618
import matplotlib.pyplot as plt
1719

18-
1920
cmaps = [('Perceptually Uniform Sequential', [
2021
'viridis', 'plasma', 'inferno', 'magma', 'cividis']),
2122
('Sequential', [
@@ -40,7 +41,6 @@
4041
'gist_rainbow', 'rainbow', 'jet', 'turbo', 'nipy_spectral',
4142
'gist_ncar'])]
4243

43-
4444
gradient = np.linspace(0, 1, 256)
4545
gradient = np.vstack((gradient, gradient))
4646

@@ -52,7 +52,7 @@ def plot_color_gradients(cmap_category, cmap_list):
5252
fig, axs = plt.subplots(nrows=nrows, figsize=(6.4, figh))
5353
fig.subplots_adjust(top=1-.35/figh, bottom=.15/figh, left=0.2, right=0.99)
5454

55-
axs[0].set_title(cmap_category + ' colormaps', fontsize=14)
55+
axs[0].set_title(f"{cmap_category} colormaps", fontsize=14)
5656

5757
for ax, cmap_name in zip(axs, cmap_list):
5858
ax.imshow(gradient, aspect='auto', cmap=cmap_name)
@@ -67,7 +67,21 @@ def plot_color_gradients(cmap_category, cmap_list):
6767
for cmap_category, cmap_list in cmaps:
6868
plot_color_gradients(cmap_category, cmap_list)
6969

70-
plt.show()
70+
71+
###############################################################################
72+
# .. _reverse-cmap:
73+
#
74+
# Reversed colormaps
75+
# ------------------
76+
#
77+
# Append ``_r`` to the name of any built-in colormap to get the reversed
78+
# version:
79+
80+
plot_color_gradients("Original and reversed ", ['viridis', 'viridis_r'])
81+
82+
# %%
83+
# The built-in reversed colormaps are generated using `.Colormap.reversed`.
84+
# For an example, see :ref:`reversing-colormap`
7185

7286
#############################################################################
7387
#

tutorials/colors/colormap-manipulation.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,43 @@ def plot_linearmap(cdict):
255255

256256
plot_examples([cmap1, cmap2])
257257

258+
#############################################################################
259+
# .. _reversing-colormap:
260+
#
261+
# Reversing a colormap
262+
# ====================
263+
#
264+
# Any colormap can be reversed using `.Colormap.reversed`. For example:
265+
266+
colors = ["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"]
267+
my_cmap = ListedColormap(colors, name="my_cmap")
268+
269+
my_cmap_r = my_cmap.reversed()
270+
271+
plot_examples([my_cmap, my_cmap_r])
272+
# %%
273+
# This method reverses the parent colormap. If no name is passed in, it also
274+
# names the reversed map by appending ``_r`` to the parent colormap's name.
275+
# See :ref:`registering-colormap` for an example.
276+
277+
##############################################################################
278+
# .. _registering-colormap:
279+
#
280+
# Registering a colormap
281+
# ======================
282+
#
283+
# Colormaps can be added to the `matplotlib.colormaps` list of named colormaps.
284+
# This allows the colormaps to be accessed by name in plotting functions:
285+
286+
mpl.colormaps.register(cmap=my_cmap)
287+
mpl.colormaps.register(cmap=my_cmap_r)
288+
289+
plot_examples(['my_cmap', 'my_cmap_r'])
290+
291+
# %%
292+
# The cmaps registered in this example were created in
293+
# :ref:`reversing-colormap`
294+
258295
#############################################################################
259296
#
260297
# .. admonition:: References

0 commit comments

Comments
 (0)