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

Skip to content

Commit 664590a

Browse files
committed
Add a dedicated ColormapRegistry class
1 parent 8676c27 commit 664590a

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

lib/matplotlib/cm.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
normalization.
1616
"""
1717

18-
from collections.abc import MutableMapping
18+
from collections.abc import Mapping, MutableMapping
19+
import copy
1920
import functools
2021

2122
import numpy as np
@@ -112,13 +113,44 @@ def _warn_deprecated(self):
112113
)
113114

114115

116+
class ColormapRegistry(Mapping):
117+
r"""
118+
Container for registered colormaps.
119+
120+
This is a read-only mapping of names to `.Colormap`\s. Use like a dict.
121+
122+
Returned `.Colormap`.s are copies, so that their modification does not
123+
change the global definition of the colormap.
124+
"""
125+
def __init__(self, cmaps):
126+
self._cmaps = cmaps
127+
128+
def __getitem__(self, item):
129+
try:
130+
return copy.deepcopy(self._cmaps[item])
131+
except KeyError:
132+
raise KeyError(f"{item} is not a known colormap name")
133+
134+
def __iter__(self):
135+
return iter(self._cmaps)
136+
137+
def __len__(self):
138+
return len(self._cmaps)
139+
140+
115141
_cmap_registry = _gen_cmap_registry()
116142
locals().update(_cmap_registry)
117143
# This is no longer considered public API
118144
cmap_d = _DeprecatedCmapDictWrapper(_cmap_registry)
145+
colormaps = ColormapRegistry(_cmap_registry)
146+
r"""
147+
Container for all registered colormaps.
119148
149+
This is a read-only mapping of names to `.Colormap`\s. Use like a dict.
120150
121-
# Continue with definitions ...
151+
Returned `.Colormap`\s are copies, so that their modification does not
152+
change the global definition of the colormap.
153+
"""
122154

123155

124156
def register_cmap(name=None, cmap=None, data=None, lut=None):

0 commit comments

Comments
 (0)