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

Commit284d9e2

Browse files
story645rutj3timhoffmjklymaktacaswell
committed
added a reversed section to colormap reference
reversing + registering section to colormap tutorialremoved parent notation from `reversed` methods b/c no heirarchyCo-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 parentb637f41 commit284d9e2

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
importnumpyasnp
1618
importmatplotlib.pyplotasplt
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
forax,cmap_nameinzip(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
forcmap_category,cmap_listincmaps:
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 forthebase class.
874874
875875
Parameters
876876
----------
877877
name : str, optional
878-
The name for the reversed colormap. Ifit'sNone the
879-
namewill be thename of the parent colormap + "_r".
878+
The name for the reversed colormap. If None, the
879+
nameis 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. Ifit'sNone the
1083-
namewill be thename of the parent colormap + "_r".
1082+
The name for the reversed colormap. If None, the
1083+
nameis 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. Ifit'sNone the
1183-
namewill be thename of the parent colormap + "_r".
1182+
The name for the reversed colormap. If None, the
1183+
nameis 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)

[8]ページ先頭

©2009-2025 Movatter.jp