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

Skip to content

Commit d28cbf2

Browse files
committed
API: remove implicit colormap generation in register_cmap
This was deprecated in 3.3 via #15875 Enforce type of cmap as Colormap because we may not know that the object registered is of the wrong type until well after it was registered, defensively check here. We could do a duck-type check, but this is much simpler and we can cross the bridge of writing a duck-type checking function when someone has a use case for registering compatible-but-not-derived-from-Colormap instances.
1 parent a899811 commit d28cbf2

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

lib/matplotlib/cm.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,27 @@ def _warn_deprecated(self):
100100
# Continue with definitions ...
101101

102102

103-
def register_cmap(name=None, cmap=None, data=None, lut=None):
103+
def register_cmap(name=None, cmap=None):
104104
"""
105105
Add a colormap to the set recognized by :func:`get_cmap`.
106106
107-
It can be used in two ways::
107+
Register a new colormap to be accessed by name ::
108108
109-
register_cmap(name='swirly', cmap=swirly_cmap)
109+
LinearSegmentedColormap('swirly', data, lut)
110+
register_cmap(cmap=swirly_cmap)
110111
111-
register_cmap(name='choppy', data=choppydata, lut=128)
112+
Parameters
113+
----------
114+
name : str, optional
115+
The name that can be used in :func:`get_cmap` or :rc:`image.cmap`
112116
113-
In the first case, *cmap* must be a :class:`matplotlib.colors.Colormap`
114-
instance. The *name* is optional; if absent, the name will
115-
be the :attr:`~matplotlib.colors.Colormap.name` attribute of the *cmap*.
117+
If absent, the name will be the :attr:`~matplotlib.colors.Colormap.name`
118+
attribute of the *cmap*.
119+
120+
cmap : matplotlib.colors.Colormap
121+
Despite being the second argument and having a default value, this
122+
is a required argument.
116123
117-
The second case is deprecated. Here, the three arguments are passed to
118-
the :class:`~matplotlib.colors.LinearSegmentedColormap` initializer,
119-
and the resulting colormap is registered. Instead of this implicit
120-
colormap creation, create a `.LinearSegmentedColormap` and use the first
121-
case: ``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.
122124
123125
Notes
124126
-----
@@ -134,23 +136,13 @@ def register_cmap(name=None, cmap=None, data=None, lut=None):
134136
except AttributeError as err:
135137
raise ValueError("Arguments must include a name or a "
136138
"Colormap") from err
137-
if isinstance(cmap, colors.Colormap):
138-
cmap._global = True
139-
_cmap_registry[name] = cmap
140-
return
141-
if lut is not None or data is not None:
142-
cbook.warn_deprecated(
143-
"3.3",
144-
message="Passing raw data via parameters data and lut to "
145-
"register_cmap() is deprecated since %(since)s and will "
146-
"become an error %(removal)s. Instead use: register_cmap("
147-
"cmap=LinearSegmentedColormap(name, data, lut))")
148-
# For the remainder, let exceptions propagate.
149-
if lut is None:
150-
lut = mpl.rcParams['image.lut']
151-
cmap = colors.LinearSegmentedColormap(name, data, lut)
139+
140+
if not isinstance(cmap, colors.Colormap):
141+
raise ValueError("You must pass a Colormap instance. "
142+
f"You passed {cmap} a {type(cmap)} object.")
152143
cmap._global = True
153144
_cmap_registry[name] = cmap
145+
return
154146

155147

156148
def get_cmap(name=None, lut=None):

lib/matplotlib/tests/test_colors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def test_register_cmap():
7272
match='Arguments must include a name or a Colormap'):
7373
cm.register_cmap()
7474

75+
with pytest.raises(ValueError, match="You must pass a Colormap instance."):
76+
cm.register_cmap('nome', cmap='not a cmap')
77+
7578

7679
def test_colormap_global_set_warn():
7780
new_cm = plt.get_cmap('viridis')

0 commit comments

Comments
 (0)