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

Skip to content

Commit 46af8cb

Browse files
story645jklymaktimhoffmrutj3tacaswell
committed
added a reversed section to colormap reference and reversing section to
colormap tutorial Co-authored-by: Jody Klymak <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]> Co-authored-by: RutgerK <[email protected]> Co-authored-by: Thomas A Caswell <[email protected]>
1 parent b637f41 commit 46af8cb

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

examples/color/colormap_reference.py

Lines changed: 22 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,23 @@ 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 when building the
84+
# `.ColormapRegistry` by calling the ``reversed`` method on the built-in
85+
# colormap objects. See :ref:`reversing-colormap` for more information about
86+
# the ``reversed`` method.
7187

7288
#############################################################################
7389
#

tutorials/colors/colormap-manipulation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,29 @@ 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 that is a registered as a matplotlib `.Colormap` can be reversed
265+
# using the ``reversed`` method of the colormap object. For example:
266+
267+
colors = ["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"]
268+
my_cmap = ListedColormap(colors, name="my_cmap")
269+
# register
270+
mpl.colormaps.register(cmap=my_cmap)
271+
# reverse custom colormap
272+
mpl.colormaps.register(cmap=my_cmap.reversed())
273+
274+
plot_examples(['my_cmap', 'my_cmap_r'])
275+
276+
# %%
277+
# This method generates a new colormap by reversing the order of colors in the
278+
# source colormap. It also creates a name for the reversed map by appending
279+
# ``_r`` to the source colormap's name.
280+
258281
#############################################################################
259282
#
260283
# .. admonition:: References

0 commit comments

Comments
 (0)