Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Backport PR #23904 on branch v3.6.x (added a reversing section to colormap reference)#24027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletionsexamples/color/colormap_reference.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,16 +6,17 @@
Reference for colormaps included with Matplotlib.

A reversed version of each of these colormaps is available by appending
``_r`` to the name,e.g., ``viridis_r``.
``_r`` to the name,as shown in :ref:`reverse-cmap`.

See :doc:`/tutorials/colors/colormaps` for an in-depth discussion about
colormaps, including colorblind-friendliness.
colormaps, including colorblind-friendliness, and
:doc:`/tutorials/colors/colormap-manipulation` for a guide to creating
colormaps.
"""

import numpy as np
import matplotlib.pyplot as plt


cmaps = [('Perceptually Uniform Sequential', [
'viridis', 'plasma', 'inferno', 'magma', 'cividis']),
('Sequential', [
Expand All@@ -40,7 +41,6 @@
'gist_rainbow', 'rainbow', 'jet', 'turbo', 'nipy_spectral',
'gist_ncar'])]


gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))

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

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

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

plt.show()

###############################################################################
# .. _reverse-cmap:
#
# Reversed colormaps
# ------------------
#
# Append ``_r`` to the name of any built-in colormap to get the reversed
# version:

plot_color_gradients("Original and reversed ", ['viridis', 'viridis_r'])

# %%
# The built-in reversed colormaps are generated using `.Colormap.reversed`.
# For an example, see :ref:`reversing-colormap`

#############################################################################
#
Expand Down
14 changes: 7 additions & 7 deletionslib/matplotlib/colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -870,13 +870,13 @@ def reversed(self, name=None):
"""
Return a reversed instance of the Colormap.

.. note:: This function is not implemented for base class.
.. note:: This function is not implemented forthebase class.

Parameters
----------
name : str, optional
The name for the reversed colormap. Ifit'sNone the
namewill be thenameof the parent colormap+ "_r".
The name for the reversed colormap. If None, the
nameis set to ``self.name + "_r"``.

See Also
--------
Expand DownExpand Up@@ -1079,8 +1079,8 @@ def reversed(self, name=None):
Parameters
----------
name : str, optional
The name for the reversed colormap. Ifit'sNone the
namewill be thenameof the parent colormap+ "_r".
The name for the reversed colormap. If None, the
nameis set to ``self.name + "_r"``.

Returns
-------
Expand DownExpand Up@@ -1179,8 +1179,8 @@ def reversed(self, name=None):
Parameters
----------
name : str, optional
The name for the reversed colormap. Ifit'sNone the
namewill be thenameof the parent colormap+ "_r".
The name for the reversed colormap. If None, the
nameis set to ``self.name + "_r"``.

Returns
-------
Expand Down
42 changes: 42 additions & 0 deletionstutorials/colors/colormap-manipulation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -255,6 +255,48 @@ def plot_linearmap(cdict):

plot_examples([cmap1, cmap2])

#############################################################################
# .. _reversing-colormap:
#
# Reversing a colormap
# ====================
#
# `.Colormap.reversed` creates a new colormap that is a reversed version of
# the original colormap.

colors = ["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"]
my_cmap = ListedColormap(colors, name="my_cmap")

my_cmap_r = my_cmap.reversed()

plot_examples([my_cmap, my_cmap_r])
# %%
# If no name is passed in, ``.reversed`` also names the copy by
# :ref:`appending '_r' <registering-colormap>` to the original colormap's
# name.

##############################################################################
# .. _registering-colormap:
#
# Registering a colormap
# ======================
#
# Colormaps can be added to the `matplotlib.colormaps` list of named colormaps.
# This allows the colormaps to be accessed by name in plotting functions:

# my_cmap, my_cmap_r from reversing a colormap
mpl.colormaps.register(cmap=my_cmap)
mpl.colormaps.register(cmap=my_cmap_r)

data = [[1, 2, 3, 4, 5]]

fig, (ax1, ax2) = plt.subplots(nrows=2)

ax1.imshow(data, cmap='my_cmap')
ax2.imshow(data, cmap='my_cmap_r')

plt.show()

#############################################################################
#
# .. admonition:: References
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp