Skip to content

Commit 284d9e2

Browse files
story645rutj3timhoffmjklymaktacaswell
committed
added a reversed section to colormap reference
reversing + registering section to colormap tutorial removed parent notation from `reversed` methods b/c no heirarchy Co-authored-by: RutgerK <2157033+RutgerK@users.noreply.github.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Jody Klymak <jklymak@gmail.com> Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
1 parent b637f41 commit 284d9e2

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

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
#

lib/matplotlib/colors.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,13 @@ def reversed(self, name=None):
870870
"""
871871
Return a reversed instance of the Colormap.
872872
873-
.. note:: This function is not implemented for base class.
873+
.. note:: This function is not implemented for the base class.
874874
875875
Parameters
876876
----------
877877
name : str, optional
878-
The name for the reversed colormap. If it's None the
879-
name will be the name of the parent colormap + "_r".
878+
The name for the reversed colormap. If None, the
879+
name is set to ```self.name``` + ```_r```.
880880
881881
See Also
882882
--------
@@ -1079,8 +1079,8 @@ def reversed(self, name=None):
10791079
Parameters
10801080
----------
10811081
name : str, optional
1082-
The name for the reversed colormap. If it's None the
1083-
name will be the name of the parent colormap + "_r".
1082+
The name for the reversed colormap. If None, the
1083+
name is set to ```self.name``` + ```_r```.
10841084
10851085
Returns
10861086
-------
@@ -1179,8 +1179,8 @@ def reversed(self, name=None):
11791179
Parameters
11801180
----------
11811181
name : str, optional
1182-
The name for the reversed colormap. If it's None the
1183-
name will be the name of the parent colormap + "_r".
1182+
The name for the reversed colormap. If None, the
1183+
name is set to ```self.name``` + ```_r```.
11841184
11851185
Returns
11861186
-------

tutorials/colors/colormap-manipulation.py

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

256256
plot_examples([cmap1, cmap2])
257257

258+
#############################################################################
259+
# .. _reversing-colormap:
260+
#
261+
# Reversing a colormap
262+
# ====================
263+
#
264+
# `.Colormap.reversed` creates a new colormap that is a reversed version of
265+
# the original colormap.
266+
267+
colors = ["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"]
268+
my_cmap = ListedColormap(colors, name="my_cmap")
269+
270+
my_cmap_r = my_cmap.reversed()
271+
272+
plot_examples([my_cmap, my_cmap_r])
273+
# %%
274+
# This method reverses the colormap and, if no name is passed in, names the
275+
# copy by appending ``_r`` to the original colormap's name.
276+
# See :ref:`registering-colormap` for an example.
277+
278+
##############################################################################
279+
# .. _registering-colormap:
280+
#
281+
# Registering a colormap
282+
# ======================
283+
#
284+
# Colormaps can be added to the `matplotlib.colormaps` list of named colormaps,
285+
# which allows the colormaps to be accessed by name in plotting functions:
286+
287+
mpl.colormaps.register(cmap=my_cmap)
288+
mpl.colormaps.register(cmap=my_cmap_r)
289+
290+
data = [[1, 2, 3, 4, 5]]
291+
292+
fig, (ax1, ax2) = plt.subplots(nrows=2)
293+
294+
ax1.imshow(data, cmap='my_cmap')
295+
ax2.imshow(data, cmap='my_cmap_r')
296+
297+
plt.show()
298+
# %%
299+
# The cmaps registered in this example were created in
300+
# :ref:`reversing-colormap`
301+
258302
#############################################################################
259303
#
260304
# .. admonition:: References

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy