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

Commit1b8866e

Browse files
committed
MNT: Remove cmap_d colormap access
1 parenta9dd8b9 commit1b8866e

File tree

6 files changed

+82
-159
lines changed

6 files changed

+82
-159
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``cmap_d`` removal
2+
~~~~~~~~~~~~~~~~~~
3+
4+
The deprecated ``matplotlib.cm.cmap_d`` access to global colormaps
5+
has been removed.

‎lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def prepare_data(d, init):
135135
continue
136136
labeled_mappables.append((label,mappable))
137137
mappables= []
138-
cmaps= [(cmap,name)forname,cmapinsorted(cm._cmap_registry.items())]
138+
cmaps= [(cmap,name)forname,cmapinsorted(cm._colormaps.items())]
139139
forlabel,mappableinlabeled_mappables:
140140
cmap=mappable.get_cmap()
141-
ifcmapnotincm._cmap_registry.values():
141+
ifcmapnotincm._colormaps.values():
142142
cmaps= [(cmap,cmap.name),*cmaps]
143143
low,high=mappable.get_clim()
144144
mappabledata= [

‎lib/matplotlib/cm.py

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

18-
fromcollections.abcimportMapping,MutableMapping
18+
fromcollections.abcimportMapping
1919

2020
importnumpyasnp
2121
fromnumpyimportma
@@ -52,52 +52,10 @@ def _gen_cmap_registry():
5252
# Generate reversed cmaps.
5353
forcmapinlist(cmap_d.values()):
5454
rmap=cmap.reversed()
55-
cmap._global=True
56-
rmap._global=True
5755
cmap_d[rmap.name]=rmap
5856
returncmap_d
5957

6058

61-
class_DeprecatedCmapDictWrapper(MutableMapping):
62-
"""Dictionary mapping for deprecated _cmap_d access."""
63-
64-
def__init__(self,cmap_registry):
65-
self._cmap_registry=cmap_registry
66-
67-
def__delitem__(self,key):
68-
self._warn_deprecated()
69-
self._cmap_registry.__delitem__(key)
70-
71-
def__getitem__(self,key):
72-
self._warn_deprecated()
73-
returnself._cmap_registry.__getitem__(key)
74-
75-
def__iter__(self):
76-
self._warn_deprecated()
77-
returnself._cmap_registry.__iter__()
78-
79-
def__len__(self):
80-
self._warn_deprecated()
81-
returnself._cmap_registry.__len__()
82-
83-
def__setitem__(self,key,val):
84-
self._warn_deprecated()
85-
self._cmap_registry.__setitem__(key,val)
86-
87-
defget(self,key,default=None):
88-
self._warn_deprecated()
89-
returnself._cmap_registry.get(key,default)
90-
91-
def_warn_deprecated(self):
92-
_api.warn_deprecated(
93-
"3.3",
94-
message="The global colormaps dictionary is no longer "
95-
"considered public API.",
96-
alternative="Please use register_cmap() and get_cmap() to "
97-
"access the contents of the dictionary."
98-
)
99-
100-
10159
classColormapRegistry(Mapping):
10260
r"""
10361
Container for colormaps that are known to Matplotlib by name.
@@ -125,6 +83,9 @@ class ColormapRegistry(Mapping):
12583
"""
12684
def__init__(self,cmaps):
12785
self._cmaps=cmaps
86+
self._builtin_cmaps=tuple(cmaps)
87+
# A shim to allow register_cmap() to force an override
88+
self._allow_override_builtin=False
12889

12990
def__getitem__(self,item):
13091
try:
@@ -177,23 +138,66 @@ def register(self, cmap, *, name=None, force=False):
177138
registered name. True supports overwriting registered colormaps
178139
other than the builtin colormaps.
179140
"""
141+
_api.check_isinstance(colors.Colormap,cmap=cmap)
142+
180143
name=nameorcmap.name
181-
ifnameinselfandnotforce:
182-
raiseValueError(
183-
f'A colormap named "{name}" is already registered.')
184-
register_cmap(name,cmap.copy())
144+
ifnameinself:
145+
ifnotforce:
146+
# don't allow registering an already existing cmap
147+
# unless explicitly asked to
148+
raiseValueError(
149+
f'A colormap named "{name}" is already registered.')
150+
elif (nameinself._builtin_cmaps
151+
andnotself._allow_override_builtin):
152+
# We don't allow overriding a builtin unless privately
153+
# coming from register_cmap()
154+
raiseValueError("Re-registering the builtin cmap "
155+
f"{name!r} is not allowed.")
156+
157+
# Warn that we are updating an already exisiting colormap
158+
_api.warn_external(f"Overwriting the cmap{name!r} "
159+
"that was already in the registry.")
160+
161+
self._cmaps[name]=cmap.copy()
185162

163+
defunregister(self,name):
164+
"""
165+
Remove a colormap from the registry.
166+
167+
You cannot remove built-in colormaps.
168+
169+
If the named colormap is not registered, returns with no error, raises
170+
if you try to de-register a default colormap.
171+
172+
.. warning::
173+
174+
Colormap names are currently a shared namespace that may be used
175+
by multiple packages. Use `unregister` only if you know you
176+
have registered that name before. In particular, do not
177+
unregister just in case to clean the name before registering a
178+
new colormap.
179+
180+
Parameters
181+
----------
182+
name : str
183+
The name of the colormap to be removed.
184+
185+
Raises
186+
------
187+
ValueError
188+
If you try to remove a default built-in colormap.
189+
"""
190+
ifnameinself._builtin_cmaps:
191+
raiseValueError(f"cannot unregister{name!r} which is a builtin "
192+
"colormap.")
193+
self._cmaps.pop(name,None)
186194

187-
_cmap_registry=_gen_cmap_registry()
188-
globals().update(_cmap_registry)
189-
# This is no longer considered public API
190-
cmap_d=_DeprecatedCmapDictWrapper(_cmap_registry)
191-
__builtin_cmaps=tuple(_cmap_registry)
192195

193196
# public access to the colormaps should be via `matplotlib.colormaps`. For now,
194197
# we still create the registry here, but that should stay an implementation
195198
# detail.
196-
_colormaps=ColormapRegistry(_cmap_registry)
199+
_colormaps=ColormapRegistry(_gen_cmap_registry())
200+
globals().update(_colormaps)
197201

198202

199203
defregister_cmap(name=None,cmap=None,*,override_builtin=False):
@@ -223,14 +227,6 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
223227
colormap.
224228
225229
Please do not use this unless you are sure you need it.
226-
227-
Notes
228-
-----
229-
Registering a colormap stores a reference to the colormap object
230-
which can currently be modified and inadvertently change the global
231-
colormap state. This behavior is deprecated and in Matplotlib 3.5
232-
the registered colormap will be immutable.
233-
234230
"""
235231
_api.check_isinstance((str,None),name=name)
236232
ifnameisNone:
@@ -239,21 +235,12 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
239235
exceptAttributeErroraserr:
240236
raiseValueError("Arguments must include a name or a "
241237
"Colormap")fromerr
242-
ifnamein_cmap_registry:
243-
ifnotoverride_builtinandnamein__builtin_cmaps:
244-
msg=f"Trying to re-register the builtin cmap{name!r}."
245-
raiseValueError(msg)
246-
else:
247-
msg=f"Trying to register the cmap{name!r} which already exists."
248-
_api.warn_external(msg)
249-
250-
ifnotisinstance(cmap,colors.Colormap):
251-
raiseValueError("You must pass a Colormap instance. "
252-
f"You passed{cmap} a{type(cmap)} object.")
253-
254-
cmap._global=True
255-
_cmap_registry[name]=cmap
256-
return
238+
# override_builtin is allowed here for backward compatbility
239+
# this is just a shim to enable that to work privately in
240+
# the global ColormapRegistry
241+
_colormaps._allow_override_builtin=override_builtin
242+
_colormaps.register(cmap,name=name,force=override_builtin)
243+
_colormaps._allow_override_builtin=False
257244

258245

259246
defget_cmap(name=None,lut=None):
@@ -263,12 +250,6 @@ def get_cmap(name=None, lut=None):
263250
Colormaps added with :func:`register_cmap` take precedence over
264251
built-in colormaps.
265252
266-
Notes
267-
-----
268-
Currently, this returns the global colormap object. This is undesired
269-
because users could accidentally modify the global colormap.
270-
From Matplotlib 3.6 on, this will return a copy instead.
271-
272253
Parameters
273254
----------
274255
name : `matplotlib.colors.Colormap` or str or None, default: None
@@ -283,11 +264,11 @@ def get_cmap(name=None, lut=None):
283264
name=mpl.rcParams['image.cmap']
284265
ifisinstance(name,colors.Colormap):
285266
returnname
286-
_api.check_in_list(sorted(_cmap_registry),name=name)
267+
_api.check_in_list(sorted(_colormaps),name=name)
287268
iflutisNone:
288-
return_cmap_registry[name]
269+
return_colormaps[name]
289270
else:
290-
return_cmap_registry[name]._resample(lut)
271+
return_colormaps[name]._resample(lut)
291272

292273

293274
defunregister_cmap(name):
@@ -321,14 +302,10 @@ def unregister_cmap(name):
321302
------
322303
ValueError
323304
If you try to de-register a default built-in colormap.
324-
325305
"""
326-
ifnamenotin_cmap_registry:
327-
return
328-
ifnamein__builtin_cmaps:
329-
raiseValueError(f"cannot unregister{name!r} which is a builtin "
330-
"colormap.")
331-
return_cmap_registry.pop(name)
306+
cmap=_colormaps.get(name,None)
307+
_colormaps.unregister(name)
308+
returncmap
332309

333310

334311
classScalarMappable:

‎lib/matplotlib/colors.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
importbase64
4343
fromcollections.abcimportSized,Sequence
44-
importcopy
4544
importfunctools
4645
importinspect
4746
importio
@@ -540,20 +539,6 @@ def _create_lookup_table(N, data, gamma=1.0):
540539
returnnp.clip(lut,0.0,1.0)
541540

542541

543-
def_warn_if_global_cmap_modified(cmap):
544-
ifgetattr(cmap,'_global',False):
545-
_api.warn_deprecated(
546-
"3.3",
547-
removal="3.6",
548-
message="You are modifying the state of a globally registered "
549-
"colormap. This has been deprecated since %(since)s and "
550-
"%(removal)s, you will not be able to modify a "
551-
"registered colormap in-place. To remove this warning, "
552-
"you can make a copy of the colormap first. "
553-
f'cmap = mpl.cm.get_cmap("{cmap.name}").copy()'
554-
)
555-
556-
557542
classColormap:
558543
"""
559544
Baseclass for all scalar to RGBA mappings.
@@ -670,7 +655,6 @@ def __copy__(self):
670655
cmapobject.__dict__.update(self.__dict__)
671656
ifself._isinit:
672657
cmapobject._lut=np.copy(self._lut)
673-
cmapobject._global=False
674658
returncmapobject
675659

676660
def__eq__(self,other):
@@ -692,7 +676,6 @@ def get_bad(self):
692676

693677
defset_bad(self,color='k',alpha=None):
694678
"""Set the color for masked values."""
695-
_warn_if_global_cmap_modified(self)
696679
self._rgba_bad=to_rgba(color,alpha)
697680
ifself._isinit:
698681
self._set_extremes()
@@ -705,7 +688,6 @@ def get_under(self):
705688

706689
defset_under(self,color='k',alpha=None):
707690
"""Set the color for low out-of-range values."""
708-
_warn_if_global_cmap_modified(self)
709691
self._rgba_under=to_rgba(color,alpha)
710692
ifself._isinit:
711693
self._set_extremes()
@@ -718,7 +700,6 @@ def get_over(self):
718700

719701
defset_over(self,color='k',alpha=None):
720702
"""Set the color for high out-of-range values."""
721-
_warn_if_global_cmap_modified(self)
722703
self._rgba_over=to_rgba(color,alpha)
723704
ifself._isinit:
724705
self._set_extremes()
@@ -741,7 +722,7 @@ def with_extremes(self, *, bad=None, under=None, over=None):
741722
values and, when ``norm.clip = False``, low (*under*) and high (*over*)
742723
out-of-range values, have been set accordingly.
743724
"""
744-
new_cm=copy.copy(self)
725+
new_cm=self.copy()
745726
new_cm.set_extremes(bad=bad,under=under,over=over)
746727
returnnew_cm
747728

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp