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

Skip to content

Commit f87100a

Browse files
committed
backport: Merge pull request #7416 from NelleV/7315_spectral
MAINT deprecated 'spectral' in favor of 'nipy_spectral' boilerplate.py was run after using 'git cherry-pick -n -m1 660aa65'
1 parent 033c3f2 commit f87100a

File tree

7 files changed

+111
-42
lines changed

7 files changed

+111
-42
lines changed

boilerplate.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ def {name}():
8787
8888
"""
8989

90+
CMAP_TEMPLATE_DEPRECATED = AUTOGEN_MSG + """
91+
def {name}():
92+
'''
93+
set the default colormap to {name} and apply to current image if any.
94+
See help(colormaps) for more information
95+
'''
96+
from matplotlib.cbook import warn_deprecated
97+
warn_deprecated(
98+
"2.0",
99+
name="{name}",
100+
obj_type="colormap"
101+
)
102+
103+
rc('image', cmap='{name}')
104+
im = gci()
105+
106+
if im is not None:
107+
im.set_cmap(cm.{name})
108+
109+
"""
110+
90111

91112
def boilerplate_gen():
92113
"""Generator of lines for the automated part of pyplot."""
@@ -322,16 +343,18 @@ def format_value(value):
322343
'spring',
323344
'summer',
324345
'winter',
325-
'spectral',
326-
327346
'magma',
328347
'inferno',
329348
'plasma',
330-
'viridis'
349+
'viridis',
350+
"nipy_spectral"
331351
)
352+
deprecated_cmaps = ("spectral", )
332353
# add all the colormaps (autumn, hsv, ....)
333354
for name in cmaps:
334355
yield CMAP_TEMPLATE.format(name=name)
356+
for name in deprecated_cmaps:
357+
yield CMAP_TEMPLATE_DEPRECATED.format(name=name)
335358

336359
yield ''
337360
yield '_setup_pyplot_info_docstrings()'

doc/users/image_tutorial.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Note that you can also change colormaps on existing plot objects using the
203203
.. sourcecode:: ipython
204204

205205
In [10]: imgplot = plt.imshow(lum_img)
206-
In [11]: imgplot.set_cmap('spectral')
206+
In [11]: imgplot.set_cmap('nipy_spectral')
207207

208208
.. plot::
209209

@@ -213,7 +213,7 @@ Note that you can also change colormaps on existing plot objects using the
213213
img = mpimg.imread('../_static/stinkbug.png')
214214
lum_img = img[:, :, 0]
215215
imgplot = plt.imshow(lum_img)
216-
imgplot.set_cmap('spectral')
216+
imgplot.set_cmap('nipy_spectral')
217217

218218
.. note::
219219

@@ -249,7 +249,7 @@ do that by adding color bars.
249249
img = mpimg.imread('../_static/stinkbug.png')
250250
lum_img = img[:, :, 0]
251251
imgplot = plt.imshow(lum_img)
252-
imgplot.set_cmap('spectral')
252+
imgplot.set_cmap('nipy_spectral')
253253
plt.colorbar()
254254

255255
This adds a colorbar to your existing figure. This won't

lib/matplotlib/_cm.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import (absolute_import, division, print_function,
1010
unicode_literals)
1111

12+
from matplotlib.cbook import warn_deprecated
1213
import numpy as np
1314

1415
_binary_data = {
@@ -1365,7 +1366,24 @@ def gfunc32(x):
13651366
)
13661367

13671368

1368-
datad = {
1369+
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+
"""
1375+
def __getitem__(self, key):
1376+
if key in ["spectral", "spectral_r"]:
1377+
warn_deprecated(
1378+
"2.0",
1379+
name="spectral and spectral_r",
1380+
alternative="nipy_spectral and nipy_spectral_r",
1381+
obj_type="colormap"
1382+
)
1383+
return super(_deprecation_datad, self).__getitem__(key)
1384+
1385+
1386+
datad = _deprecation_datad({
13691387
'afmhot': _afmhot_data,
13701388
'autumn': _autumn_data,
13711389
'bone': _bone_data,
@@ -1394,7 +1412,7 @@ def gfunc32(x):
13941412
'winter': _winter_data,
13951413
'nipy_spectral': _nipy_spectral_data,
13961414
'spectral': _nipy_spectral_data, # alias for backward compatibility
1397-
}
1415+
})
13981416

13991417

14001418
datad['Blues'] = _Blues_data

lib/matplotlib/cm.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
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
1717
import matplotlib.colors as colors
1818
import matplotlib.cbook as cbook
19-
from matplotlib._cm import datad
19+
from matplotlib._cm import datad, _deprecation_datad
2020
from matplotlib._cm import cubehelix
2121
from matplotlib._cm_listed import cmaps as cmaps_listed
2222

23-
cmap_d = dict()
23+
cmap_d = _deprecation_datad()
2424

2525
# reverse all the colormaps.
2626
# reversed colormaps have '_r' appended to the name.
@@ -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

lib/matplotlib/pylab.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
spring - set the default colormap to spring
122122
summer - set the default colormap to summer
123123
winter - set the default colormap to winter
124-
spectral - set the default colormap to spectral
125124
126125
_Event handling
127126

lib/matplotlib/pyplot.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,7 +2698,8 @@ def boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,
26982698
conf_intervals=None, meanline=None, showmeans=None, showcaps=None,
26992699
showbox=None, showfliers=None, boxprops=None, labels=None,
27002700
flierprops=None, medianprops=None, meanprops=None, capprops=None,
2701-
whiskerprops=None, manage_xticks=True, hold=None, data=None):
2701+
whiskerprops=None, manage_xticks=True, autorange=False, zorder=None,
2702+
hold=None, data=None):
27022703
ax = gca()
27032704
# allow callers to override the hold state by passing hold=True|False
27042705
washold = ax.ishold()
@@ -2717,7 +2718,8 @@ def boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,
27172718
flierprops=flierprops, medianprops=medianprops,
27182719
meanprops=meanprops, capprops=capprops,
27192720
whiskerprops=whiskerprops,
2720-
manage_xticks=manage_xticks, data=data)
2721+
manage_xticks=manage_xticks, autorange=autorange,
2722+
zorder=zorder, data=data)
27212723
finally:
27222724
ax.hold(washold)
27232725

@@ -2950,7 +2952,7 @@ def hexbin(x, y, C=None, gridsize=100, bins=None, xscale='linear',
29502952
# This function was autogenerated by boilerplate.py. Do not edit as
29512953
# changes will be lost
29522954
@_autogen_docstring(Axes.hist)
2953-
def hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False,
2955+
def hist(x, bins=None, range=None, normed=False, weights=None, cumulative=False,
29542956
bottom=None, histtype='bar', align='mid', orientation='vertical',
29552957
rwidth=None, log=False, color=None, label=None, stacked=False,
29562958
hold=None, data=None, **kwargs):
@@ -3378,7 +3380,8 @@ def step(x, y, *args, **kwargs):
33783380
@_autogen_docstring(Axes.streamplot)
33793381
def streamplot(x, y, u, v, density=1, linewidth=None, color=None, cmap=None,
33803382
norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1,
3381-
transform=None, zorder=2, start_points=None, hold=None, data=None):
3383+
transform=None, zorder=None, start_points=None, hold=None,
3384+
data=None):
33823385
ax = gca()
33833386
# allow callers to override the hold state by passing hold=True|False
33843387
washold = ax.ishold()
@@ -3815,20 +3818,6 @@ def winter():
38153818
im.set_cmap(cm.winter)
38163819

38173820

3818-
# This function was autogenerated by boilerplate.py. Do not edit as
3819-
# changes will be lost
3820-
def spectral():
3821-
'''
3822-
set the default colormap to spectral and apply to current image if any.
3823-
See help(colormaps) for more information
3824-
'''
3825-
rc('image', cmap='spectral')
3826-
im = gci()
3827-
3828-
if im is not None:
3829-
im.set_cmap(cm.spectral)
3830-
3831-
38323821
# This function was autogenerated by boilerplate.py. Do not edit as
38333822
# changes will be lost
38343823
def magma():
@@ -3884,4 +3873,39 @@ def viridis():
38843873
if im is not None:
38853874
im.set_cmap(cm.viridis)
38863875

3876+
3877+
# This function was autogenerated by boilerplate.py. Do not edit as
3878+
# changes will be lost
3879+
def nipy_spectral():
3880+
'''
3881+
set the default colormap to nipy_spectral and apply to current image if any.
3882+
See help(colormaps) for more information
3883+
'''
3884+
rc('image', cmap='nipy_spectral')
3885+
im = gci()
3886+
3887+
if im is not None:
3888+
im.set_cmap(cm.nipy_spectral)
3889+
3890+
3891+
# This function was autogenerated by boilerplate.py. Do not edit as
3892+
# changes will be lost
3893+
def spectral():
3894+
'''
3895+
set the default colormap to spectral and apply to current image if any.
3896+
See help(colormaps) for more information
3897+
'''
3898+
from matplotlib.cbook import warn_deprecated
3899+
warn_deprecated(
3900+
"2.0",
3901+
name="spectral",
3902+
obj_type="colormap"
3903+
)
3904+
3905+
rc('image', cmap='spectral')
3906+
im = gci()
3907+
3908+
if im is not None:
3909+
im.set_cmap(cm.spectral)
3910+
38873911
_setup_pyplot_info_docstrings()

lib/mpl_toolkits/tests/test_axes_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_imagegrid_cbar_mode_edge():
2929
cbar_mode='edge')
3030
ax1, ax2, ax3, ax4, = grid
3131

32-
im1 = ax1.imshow(arr.real, cmap='spectral')
32+
im1 = ax1.imshow(arr.real, cmap='nipy_spectral')
3333
im2 = ax2.imshow(arr.imag, cmap='hot')
3434
im3 = ax3.imshow(np.abs(arr), cmap='jet')
3535
im4 = ax4.imshow(np.arctan2(arr.imag, arr.real), cmap='hsv')

0 commit comments

Comments
 (0)