Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce3e284

Browse filesBrowse 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 ce3e284
Copy full SHA for ce3e284

File tree

Expand file treeCollapse file tree

3 files changed

+69
-13
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+69
-13
lines changed

‎examples/color/colormap_reference.py

Copy file name to clipboardExpand all lines: examples/color/colormap_reference.py
+20-6Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+7-7Lines 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

Copy file name to clipboardExpand all lines: tutorials/colors/colormap-manipulation.py
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,48 @@ 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. If no name is passed in, it also names
275+
# the copy by :ref:`appending '_r' <registering-colormap>` to the original
276+
# colormap's name.
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+
# my_cmap, my_cmap_r from reversing a colormap
288+
mpl.colormaps.register(cmap=my_cmap)
289+
mpl.colormaps.register(cmap=my_cmap_r)
290+
291+
data = [[1, 2, 3, 4, 5]]
292+
293+
fig, (ax1, ax2) = plt.subplots(nrows=2)
294+
295+
ax1.imshow(data, cmap='my_cmap')
296+
ax2.imshow(data, cmap='my_cmap_r')
297+
298+
plt.show()
299+
258300
#############################################################################
259301
#
260302
# .. admonition:: References

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.