10000 MAINT catches deprecation warning when importing cm.py · matplotlib/matplotlib@c5738e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5738e2

Browse files
committed
MAINT catches deprecation warning when importing cm.py
We don't want to raise the deprecation warning for spectral/spectral_r unless the user calls that colormap.
1 parent 6d7aaee commit c5738e2

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

lib/matplotlib/_cm.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,12 +1367,17 @@ def gfunc32(x):
13671367

13681368

13691369
class _deprecation_datad(dict):
1370+
"""
1371+
This class only exists for the purpose of raising an appropriate warning
1372+
for the deprecation of spectral. It should be remove in 2.2, once the
1373+
colormap spectral disappears.
1374+
"""
13701375
def __getitem__(self, key):
13711376
if key in ["spectral", "spectral_r"]:
13721377
warn_deprecated(
13731378
"2.0",
1374-
name="spectral",
1375-
alternative="nipy_spectral",
1379+
name="spectral and spectral_r",
1380+
alternative="nipy_spectral and nipy_spectral_r",
13761381
obj_type="colormap"
13771382
)
13781383
return super(_deprecation_datad, self).__getitem__(key)

lib/matplotlib/cm.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import six
1111

1212
import os
13-
13+
import warnings as _warnings # To remove once spectral is removed
1414
import numpy as np
1515
from numpy import ma
1616
import matplotlib as mpl
@@ -81,17 +81,22 @@ def _generate_cmap(name, lutsize):
8181

8282
LUTSIZE = mpl.rcParams['image.lut']
8383

84-
# Generate the reversed specifications ...
85-
for cmapname in list(six.iterkeys(datad)):
86-
spec = datad[cmapname]
87-
spec_reversed = _reverse_cmap_spec(spec)
88-
datad[cmapname + '_r'] = spec_reversed
89-
90-
# Precache the cmaps with ``lutsize = LUTSIZE`` ...
91-
92-
# Use datad.keys() to also add the reversed ones added in the section above:
93-
for cmapname in six.iterkeys(datad):
94-
cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
84+
# We silence warnings here to avoid raising the deprecation warning for
85+
# spectral/spectral_r when this module is imported.
86+
with _warnings.catch_warnings():
87+
_warnings.simplefilter("ignore")
88+
# Generate the reversed specifications ...
89+
for cmapname in list(six.iterkeys(datad)):
90+
spec = datad[cmapname]
91+
spec_reversed = _reverse_cmap_spec(spec)
92+
datad[cmapname + '_r'] = spec_reversed
93+
94+
# Precache the cmaps with ``lutsize = LUTSIZE`` ...
95+
96+
# Use datad.keys() to also add the reversed ones added in the section
97+
# above:
98+
for cmapname in six.iterkeys(datad):
99+
cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
95100

96101
cmap_d.update(cmaps_listed)
97102

0 commit comments

Comments
 (0)
0