Skip to content

Commit

Permalink
backport: Merge pull request #7416 from NelleV/7315_spectral
Browse files Browse the repository at this point in the history
MAINT deprecated 'spectral' in favor of 'nipy_spectral'

boilerplate.py was run after using 'git cherry-pick -n -m1 660aa65'
  • Loading branch information
efiring committed Nov 10, 2016
1 parent 033c3f2 commit f87100a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 42 deletions.
29 changes: 26 additions & 3 deletions boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ def {name}():
"""

CMAP_TEMPLATE_DEPRECATED = AUTOGEN_MSG + """
def {name}():
'''
set the default colormap to {name} and apply to current image if any.
See help(colormaps) for more information
'''
from matplotlib.cbook import warn_deprecated
warn_deprecated(
"2.0",
name="{name}",
obj_type="colormap"
)
rc('image', cmap='{name}')
im = gci()
if im is not None:
im.set_cmap(cm.{name})
"""


def boilerplate_gen():
"""Generator of lines for the automated part of pyplot."""
Expand Down Expand Up @@ -322,16 +343,18 @@ def format_value(value):
'spring',
'summer',
'winter',
'spectral',

'magma',
'inferno',
'plasma',
'viridis'
'viridis',
"nipy_spectral"
)
deprecated_cmaps = ("spectral", )
# add all the colormaps (autumn, hsv, ....)
for name in cmaps:
yield CMAP_TEMPLATE.format(name=name)
for name in deprecated_cmaps:
yield CMAP_TEMPLATE_DEPRECATED.format(name=name)

yield ''
yield '_setup_pyplot_info_docstrings()'
Expand Down
6 changes: 3 additions & 3 deletions doc/users/image_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Note that you can also change colormaps on existing plot objects using the
.. sourcecode:: ipython

In [10]: imgplot = plt.imshow(lum_img)
In [11]: imgplot.set_cmap('spectral')
In [11]: imgplot.set_cmap('nipy_spectral')

.. plot::

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

.. note::

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

This adds a colorbar to your existing figure. This won't
Expand Down
22 changes: 20 additions & 2 deletions lib/matplotlib/_cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from matplotlib.cbook import warn_deprecated
import numpy as np

_binary_data = {
Expand Down Expand Up @@ -1365,7 +1366,24 @@ def gfunc32(x):
)


datad = {
class _deprecation_datad(dict):
"""
This class only exists for the purpose of raising an appropriate warning
for the deprecation of spectral. It should be remove in 2.2, once the
colormap spectral disappears.
"""
def __getitem__(self, key):
if key in ["spectral", "spectral_r"]:
warn_deprecated(
"2.0",
name="spectral and spectral_r",
alternative="nipy_spectral and nipy_spectral_r",
obj_type="colormap"
)
return super(_deprecation_datad, self).__getitem__(key)


datad = _deprecation_datad({
'afmhot': _afmhot_data,
'autumn': _autumn_data,
'bone': _bone_data,
Expand Down Expand Up @@ -1394,7 +1412,7 @@ def gfunc32(x):
'winter': _winter_data,
'nipy_spectral': _nipy_spectral_data,
'spectral': _nipy_spectral_data, # alias for backward compatibility
}
})


datad['Blues'] = _Blues_data
Expand Down
33 changes: 19 additions & 14 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import six

import os

import warnings as _warnings # To remove once spectral is removed
import numpy as np
from numpy import ma
import matplotlib as mpl
import matplotlib.colors as colors
import matplotlib.cbook as cbook
from matplotlib._cm import datad
from matplotlib._cm import datad, _deprecation_datad
from matplotlib._cm import cubehelix
from matplotlib._cm_listed import cmaps as cmaps_listed

cmap_d = dict()
cmap_d = _deprecation_datad()

# reverse all the colormaps.
# reversed colormaps have '_r' appended to the name.
Expand Down Expand Up @@ -81,17 +81,22 @@ def _generate_cmap(name, lutsize):

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

# Generate the reversed specifications ...
for cmapname in list(six.iterkeys(datad)):
spec = datad[cmapname]
spec_reversed = _reverse_cmap_spec(spec)
datad[cmapname + '_r'] = spec_reversed

# Precache the cmaps with ``lutsize = LUTSIZE`` ...

# Use datad.keys() to also add the reversed ones added in the section above:
for cmapname in six.iterkeys(datad):
cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)
# We silence warnings here to avoid raising the deprecation warning for
# spectral/spectral_r when this module is imported.
with _warnings.catch_warnings():
_warnings.simplefilter("ignore")
# Generate the reversed specifications ...
for cmapname in list(six.iterkeys(datad)):
spec = datad[cmapname]
spec_reversed = _reverse_cmap_spec(spec)
datad[cmapname + '_r'] = spec_reversed

# Precache the cmaps with ``lutsize = LUTSIZE`` ...

# Use datad.keys() to also add the reversed ones added in the section
# above:
for cmapname in six.iterkeys(datad):
cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE)

cmap_d.update(cmaps_listed)

Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/pylab.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
spring - set the default colormap to spring
summer - set the default colormap to summer
winter - set the default colormap to winter
spectral - set the default colormap to spectral
_Event handling
Expand Down
60 changes: 42 additions & 18 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,7 +2698,8 @@ def boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,
conf_intervals=None, meanline=None, showmeans=None, showcaps=None,
showbox=None, showfliers=None, boxprops=None, labels=None,
flierprops=None, medianprops=None, meanprops=None, capprops=None,
whiskerprops=None, manage_xticks=True, hold=None, data=None):
whiskerprops=None, manage_xticks=True, autorange=False, zorder=None,
hold=None, data=None):
ax = gca()
# allow callers to override the hold state by passing hold=True|False
washold = ax.ishold()
Expand All @@ -2717,7 +2718,8 @@ def boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None,
flierprops=flierprops, medianprops=medianprops,
meanprops=meanprops, capprops=capprops,
whiskerprops=whiskerprops,
manage_xticks=manage_xticks, data=data)
manage_xticks=manage_xticks, autorange=autorange,
zorder=zorder, data=data)
finally:
ax.hold(washold)

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


# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
def spectral():
'''
set the default colormap to spectral and apply to current image if any.
See help(colormaps) for more information
'''
rc('image', cmap='spectral')
im = gci()

if im is not None:
im.set_cmap(cm.spectral)


# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
def magma():
Expand Down Expand Up @@ -3884,4 +3873,39 @@ def viridis():
if im is not None:
im.set_cmap(cm.viridis)


# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
def nipy_spectral():
'''
set the default colormap to nipy_spectral and apply to current image if any.
See help(colormaps) for more information
'''
rc('image', cmap='nipy_spectral')
im = gci()

if im is not None:
im.set_cmap(cm.nipy_spectral)


# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
def spectral():
'''
set the default colormap to spectral and apply to current image if any.
See help(colormaps) for more information
'''
from matplotlib.cbook import warn_deprecated
warn_deprecated(
"2.0",
name="spectral",
obj_type="colormap"
)

rc('image', cmap='spectral')
im = gci()

if im is not None:
im.set_cmap(cm.spectral)

_setup_pyplot_info_docstrings()
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/tests/test_axes_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_imagegrid_cbar_mode_edge():
cbar_mode='edge')
ax1, ax2, ax3, ax4, = grid

im1 = ax1.imshow(arr.real, cmap='spectral')
im1 = ax1.imshow(arr.real, cmap='nipy_spectral')
im2 = ax2.imshow(arr.imag, cmap='hot')
im3 = ax3.imshow(np.abs(arr), cmap='jet')
im4 = ax4.imshow(np.arctan2(arr.imag, arr.real), cmap='hsv')
Expand Down

0 comments on commit f87100a

Please sign in to comment.