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

Skip to content

Commit a8a4f7f

Browse files
committed
Removed tests, updated documentation with code style for removed functions, moved code to pyplot from cm
1 parent c8d70ac commit a8a4f7f

File tree

13 files changed

+63
-133
lines changed

13 files changed

+63
-133
lines changed

ci/mypy-stubtest-allowlist.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ matplotlib.figure.Figure.set_constrained_layout
4949
matplotlib.figure.Figure.set_constrained_layout_pads
5050
matplotlib.figure.Figure.set_tight_layout
5151

52-
# 3.7 deprecations
53-
5452
# 3.8 deprecations
5553
matplotlib.cbook.get_sample_data
5654
matplotlib.contour.ContourSet.allkinds
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
``matplotlib.cm.register_cmap``
2-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1+
In Matplotlib 3.5 we added a `.ColormapRegistry` class and exposed an instance
2+
at the top level as ``matplotlib.colormaps``. The existing top level functions
3+
in `matplotlib.cm` (``get_cmap``, ``register_cmap``, ``unregister_cmap``) were
4+
changed to be aliases around the same instance. In Matplotlib 3.6 we have
5+
marked those top level functions as pending deprecation.
36

4-
... was removed as it was deprecated during 3.7. Use `matplotlib.colormaps.register` instead.
7+
In Matplotlib 3.7, the following functions have been marked for deprecation:
58

6-
``matplotlib.cm.unregister_cmap``
7-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
- ``matplotlib.cm.get_cmap``; use ``matplotlib.colormaps[name]`` instead if you
10+
have a `str`.
811

9-
... was removed as it was deprecated during 3.7. Use `matplotlib.colormaps.unregister` instead.
12+
**Added 3.6.1** Use `matplotlib.cm.ColormapRegistry.get_cmap` if you
13+
have a string, `None` or a `matplotlib.colors.Colormap` object that you want
14+
to convert to a `matplotlib.colors.Colormap` instance.
15+
- ``matplotlib.cm.register_cmap``; use `matplotlib.colormaps.register
16+
<.ColormapRegistry.register>` instead
17+
- ``matplotlib.cm.unregister_cmap``; use `matplotlib.colormaps.unregister
18+
<.ColormapRegistry.unregister>` instead
19+
- ``matplotlib.pyplot.register_cmap``; use `matplotlib.colormaps.register
20+
<.ColormapRegistry.register>` instead
1021

11-
``matplotlib.pyplot.register_cmap``
12-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
14-
... was removed as it was deprecated during 3.7. Use `matplotlib.colormaps.register` instead.
15-
16-
``matplotlib.cm.get_cmap``
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~
18-
19-
... was removed as it was deprecated during 3.7. Use `matplotlib.cm.ColormapRegistry.get_cmap` if you have a string, `None`
20-
or a `matplotlib.colors.Colormap` object that you want to convert to a `matplotlib.colors.Colormap` instance.
22+
The `matplotlib.pyplot.get_cmap` function will stay available for backward
23+
compatibility.

doc/api/prev_api_changes/api_changes_0.99.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Changes in 0.99
77
NumPy arrays.
88

99
* User-generated colormaps can now be added to the set recognized
10-
by :func:`matplotlib.cm.get_cmap`. Colormaps can be made the
10+
by ``matplotlib.cm.get_cmap``. Colormaps can be made the
1111
default and applied to the current image using
1212
:func:`matplotlib.pyplot.set_cmap`.
1313

doc/api/prev_api_changes/api_changes_3.2.0/behavior.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Exception changes
294294
~~~~~~~~~~~~~~~~~
295295
Various APIs that raised a `ValueError` for incorrectly typed inputs now raise
296296
`TypeError` instead: `.backend_bases.GraphicsContextBase.set_clip_path`,
297-
``blocking_input.BlockingInput.__call__``, `.cm.register_cmap`, `.dviread.DviFont`,
297+
``blocking_input.BlockingInput.__call__``, ``matplotlib.cm.register_cmap``, `.dviread.DviFont`,
298298
`.rcsetup.validate_hatch`, ``.rcsetup.validate_animation_writer_path``, `.spines.Spine`,
299299
many classes in the :mod:`matplotlib.transforms` module and :mod:`matplotlib.tri`
300300
package, and Axes methods that take a ``norm`` parameter.

doc/api/prev_api_changes/api_changes_3.3.0/deprecations.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Please pass capstyles ("miter", "round", "bevel") and joinstyles ("butt",
5555

5656
Passing raw data to ``register_cmap()``
5757
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58-
Passing raw data via parameters *data* and *lut* to `.register_cmap()` is
58+
Passing raw data via parameters *data* and *lut* to ``matplotlib.cm.register_cmap()`` is
5959
deprecated. Instead, explicitly create a `.LinearSegmentedColormap` and pass
6060
it via the *cmap* parameter:
6161
``register_cmap(cmap=LinearSegmentedColormap(name, data, lut))``.

doc/api/prev_api_changes/api_changes_3.4.0/behaviour.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ time, not at draw time.
203203
Raise or warn on registering a colormap twice
204204
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205205

206-
When using `matplotlib.cm.register_cmap` to register a user provided or
206+
When using ``matplotlib.cm.register_cmap`` to register a user provided or
207207
third-party colormap it will now raise a `ValueError` if trying to over-write
208208
one of the built in colormaps and warn if trying to over write a user
209209
registered colormap. This may raise for user-registered colormaps in the

doc/api/prev_api_changes/api_changes_3.6.0/behaviour.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Behaviour changes
44
``plt.get_cmap`` and ``matplotlib.cm.get_cmap`` return a copy
55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66

7-
Formerly, `~.pyplot.get_cmap` and `.cm.get_cmap` returned a global version of a
7+
Formerly, `~.pyplot.get_cmap` and ``matplotlib.cm.get_cmap`` returned a global version of a
88
`.Colormap`. This was prone to errors as modification of the colormap would
99
propagate from one location to another without warning. Now, a new copy of the
1010
colormap is returned.

doc/api/prev_api_changes/api_changes_3.7.0/behaviour.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ also be based on ``mpl_toolkits.axisartist``. This behavior is consistent with
2020
``plt.get_cmap`` and ``matplotlib.cm.get_cmap`` return a copy
2121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2222

23-
Formerly, `~.pyplot.get_cmap` and `.cm.get_cmap` returned a global version of a
23+
Formerly, `~.pyplot.get_cmap` and ``matplotlib.cm.get_cmap`` returned a global version of a
2424
`.Colormap`. This was prone to errors as modification of the colormap would
2525
propagate from one location to another without warning. Now, a new copy of the
2626
colormap is returned.

doc/users/prev_whats_new/whats_new_3.4.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ for out-of-range and masked values.
547547
New ``cm.unregister_cmap`` function
548548
-----------------------------------
549549

550-
`.cm.unregister_cmap` allows users to remove a colormap that they have
550+
``matplotlib.cm.unregister_cmap`` allows users to remove a colormap that they have
551551
previously registered.
552552

553553
New ``CenteredNorm`` for symmetrical data around a center

doc/users/prev_whats_new/whats_new_3.5.0.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ To register new colormaps use::
148148

149149
plt.colormaps.register(my_colormap)
150150

151-
We recommend to use the new API instead of the `~.cm.get_cmap` and
152-
`~.cm.register_cmap` functions for new code. `matplotlib.cm.get_cmap` and
153-
`matplotlib.cm.register_cmap` will eventually be deprecated and removed.
151+
We recommend to use the new API instead of the ``matplotlib.cm.get_cmap`` and
152+
``matplotlib.cm.register_cmap`` functions for new code. ``matplotlib.cm.get_cmap`` and
153+
``matplotlib.cm.register_cmap`` will eventually be deprecated and removed.
154154
Within `.pyplot`, ``plt.get_cmap()`` and ``plt.register_cmap()`` will continue
155155
to be supported for backward compatibility.
156156

lib/matplotlib/cm.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ class ColormapRegistry(Mapping):
7878
def __init__(self, cmaps):
7979
self._cmaps = cmaps
8080
self._builtin_cmaps = tuple(cmaps)
81-
# A shim to allow register_cmap() to force an override
82-
self._allow_override_builtin = False
8381

8482
def __getitem__(self, item):
8583
try:
@@ -141,10 +139,8 @@ def register(self, cmap, *, name=None, force=False):
141139
# unless explicitly asked to
142140
raise ValueError(
143141
f'A colormap named "{name}" is already registered.')
144-
elif (name in self._builtin_cmaps
145-
and not self._allow_override_builtin):
146-
# We don't allow overriding a builtin unless privately
147-
# coming from register_cmap()
142+
elif name in self._builtin_cmap:
143+
# We don't allow overriding a builtin
148144
raise ValueError("Re-registering the builtin cmap "
149145
f"{name!r} is not allowed.")
150146

@@ -231,35 +227,6 @@ def get_cmap(self, cmap):
231227
globals().update(_colormaps)
232228

233229

234-
def _get_cmap(name=None, lut=None):
235-
"""
236-
Get a colormap instance, defaulting to rc values if *name* is None.
237-
238-
Parameters
239-
----------
240-
name : `~matplotlib.colors.Colormap` or str or None, default: None
241-
If a `.Colormap` instance, it will be returned. Otherwise, the name of
242-
a colormap known to Matplotlib, which will be resampled by *lut*. The
243-
default, None, means :rc:`image.cmap`.
244-
lut : int or None, default: None
245-
If *name* is not already a Colormap instance and *lut* is not None, the
246-
colormap will be resampled to have *lut* entries in the lookup table.
247-
248-
Returns
249-
-------
250-
Colormap
251-
"""
252-
if name is None:
253-
name = mpl.rcParams['image.cmap']
254-
if isinstance(name, colors.Colormap):
255-
return name
256-
_api.check_in_list(sorted(_colormaps), name=name)
257-
if lut is None:
258-
return _colormaps[name]
259-
else:
260-
return _colormaps[name].resampled(lut)
261-
262-
263230
def _auto_norm_from_scale(scale_cls):
264231
"""
265232
Automatically generate a norm class from *scale_cls*.

lib/matplotlib/pyplot.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
import matplotlib
5656
import matplotlib.colorbar
5757
import matplotlib.image
58-
from matplotlib import _api
58+
import matplotlib as mpl
59+
from matplotlib import _api, colors
5960
from matplotlib import ( # Re-exported for typing.
6061
cm as cm, get_backend as get_backend, rcParams as rcParams, style as style)
6162
from matplotlib import _pylab_helpers, interactive
@@ -2347,7 +2348,36 @@ def clim(vmin: float | None = None, vmax: float | None = None) -> None:
23472348

23482349

23492350
# eventually this implementation should move here, use indirection for now to
2350-
# avoid having two copies of the code floating around.
2351+
# avoid having two copies of the code floating around.
2352+
def _get_cmap(name=None, lut=None):
2353+
"""
2354+
Get a colormap instance, defaulting to rc values if *name* is None.
2355+
2356+
Parameters
2357+
----------
2358+
name : `~matplotlib.colors.Colormap` or str or None, default: None
2359+
If a `.Colormap` instance, it will be returned. Otherwise, the name of
2360+
a colormap known to Matplotlib, which will be resampled by *lut*. The
2361+
default, None, means :rc:`image.cmap`.
2362+
lut : int or None, default: None
2363+
If *name* is not already a Colormap instance and *lut* is not None, the
2364+
colormap will be resampled to have *lut* entries in the lookup table.
2365+
2366+
Returns
2367+
-------
2368+
Colormap
2369+
"""
2370+
if name is None:
2371+
name = mpl.rcParams['image.cmap']
2372+
if isinstance(name, colors.Colormap):
2373+
return name
2374+
_api.check_in_list(sorted(_colormaps), name=name)
2375+
if lut is None:
2376+
return _colormaps[name]
2377+
else:
2378+
return _colormaps[name].resampled(lut)
2379+
2380+
23512381
def get_cmap(
23522382
name: Colormap | str | None = None,
23532383
lut: int | None = None

lib/matplotlib/tests/test_colors.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -72,51 +72,6 @@ def test_resampled():
7272
assert_array_almost_equal(lc(np.nan), lc3(np.nan))
7373

7474

75-
def test_register_cmap():
76-
new_cm = mpl.colormaps["viridis"]
77-
target = "viridis2"
78-
with pytest.warns(
79-
mpl.MatplotlibDeprecationWarning,
80-
match=r"matplotlib\.colormaps\.register\(name\)"
81-
):
82-
cm.register_cmap(target, new_cm)
83-
assert mpl.colormaps[target] == new_cm
84-
85-
with pytest.raises(ValueError,
86-
match="Arguments must include a name or a Colormap"):
87-
with pytest.warns(
88-
mpl.MatplotlibDeprecationWarning,
89-
match=r"matplotlib\.colormaps\.register\(name\)"
90-
):
91-
cm.register_cmap()
92-
93-
with pytest.warns(
94-
mpl.MatplotlibDeprecationWarning,
95-
match=r"matplotlib\.colormaps\.unregister\(name\)"
96-
):
97-
cm.unregister_cmap(target)
98-
with pytest.raises(ValueError,
99-
match=f'{target!r} is not a valid value for name;'):
100-
with pytest.warns(
101-
mpl.MatplotlibDeprecationWarning,
102-
match=r"matplotlib\.colormaps\[name\]"
103-
):
104-
cm.get_cmap(target)
105-
with pytest.warns(
106-
mpl.MatplotlibDeprecationWarning,
107-
match=r"matplotlib\.colormaps\.unregister\(name\)"
108-
):
109-
# test that second time is error free
110-
cm.unregister_cmap(target)
111-
112-
with pytest.raises(TypeError, match="'cmap' must be"):
113-
with pytest.warns(
114-
mpl.MatplotlibDeprecationWarning,
115-
match=r"matplotlib\.colormaps\.register\(name\)"
116-
):
117-
cm.register_cmap('nome', cmap='not a cmap')
118-
119-
12075
def test_colormaps_get_cmap():
12176
cr = mpl.colormaps
12277

@@ -137,29 +92,6 @@ def test_colormaps_get_cmap():
13792
cr.get_cmap(object())
13893

13994

140-
def test_double_register_builtin_cmap():
141-
name = "viridis"
142-
match = f"Re-registering the builtin cmap {name!r}."
143-
with pytest.raises(ValueError, match=match):
144-
matplotlib.colormaps.register(
145-
mpl.colormaps[name], name=name, force=True
146-
)
147-
with pytest.raises(ValueError, match='A colormap named "viridis"'):
148-
with pytest.warns(mpl.MatplotlibDeprecationWarning):
149-
cm.register_cmap(name, mpl.colormaps[name])
150-
with pytest.warns(UserWarning):
151-
# TODO is warning more than once!
152-
cm.register_cmap(name, mpl.colormaps[name], override_builtin=True)
153-
154-
155-
def test_unregister_builtin_cmap():
156-
name = "viridis"
157-
match = f'cannot unregister {name!r} which is a builtin colormap.'
158-
with pytest.raises(ValueError, match=match):
159-
with pytest.warns(mpl.MatplotlibDeprecationWarning):
160-
cm.unregister_cmap(name)
161-
162-
16395
def test_colormap_copy():
16496
cmap = plt.cm.Reds
16597
copied_cmap = copy.copy(cmap)

0 commit comments

Comments
 (0)