X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/users/whats_new/reversed_colormap.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Colormap reversed method
------------------------

The methods :meth:`~matplotlib.colors.LinearSegmentedColormap.reversed` and
:meth:`~matplotlib.colors.ListedColormap.reversed` return a reversed
instance of the Colormap. This implements a way for any Colormap to be
reversed.
74 changes: 74 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,25 @@ def _resample(self, lutsize):
"""
raise NotImplementedError()

def reversed(self, name=None):
"""
Make a reversed instance of the Colormap.

.. note :: Function not implemented for base class.

Parameters
----------
name : str, optional
The name for the reversed colormap. If it's None the
name will be the name of the parent colormap + "_r".

Notes
-----
See :meth:`LinearSegmentedColormap.reversed` and
:meth:`ListedColormap.reversed`
"""
raise NotImplementedError()


class LinearSegmentedColormap(Colormap):
"""Colormap objects based on lookup tables using linear segments.
Expand Down Expand Up @@ -784,6 +803,40 @@ def _resample(self, lutsize):
"""
return LinearSegmentedColormap(self.name, self._segmentdata, lutsize)

def reversed(self, name=None):
"""
Make a reversed instance of the Colormap.

Parameters
----------
name : str, optional
The name for the reversed colormap. If it's None the
name will be the name of the parent colormap + "_r".

Returns
-------
LinearSegmentedColormap
The reversed colormap.
"""
if name is None:
name = self.name + "_r"

# Function factory needed to deal with 'late binding' issue.
def factory(dat):
def func_r(x):
return dat(1.0 - x)
return func_r

data_r = dict()
for key, data in six.iteritems(self._segmentdata):
if six.callable(data):
data_r[key] = factory(data)
else:
new_data = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)]
data_r[key] = new_data

return LinearSegmentedColormap(name, data_r, self.N, self._gamma)


class ListedColormap(Colormap):
"""Colormap object generated from a list of colors.
Expand Down Expand Up @@ -856,6 +909,27 @@ def _resample(self, lutsize):
colors = self(np.linspace(0, 1, lutsize))
return ListedColormap(colors, name=self.name)

def reversed(self, name=None):
"""
Make a reversed instance of the Colormap.

Parameters
----------
name : str, optional
The name for the reversed colormap. If it's None the
name will be the name of the parent colormap + "_r".

Returns
-------
ListedColormap
A reversed instance of the colormap.
"""
if name is None:
name = self.name + "_r"

colors_r = list(reversed(self.colors))
return ListedColormap(colors_r, name=name, N=self.N)


class Normalize(object):
"""
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ def test_pandas_iterable():
assert_sequence_equal(cm1.colors, cm2.colors)


def test_colormap_reversing():
"""Check the generated _lut data of a colormap and corresponding
reversed colormap if they are almost the same."""
for name in six.iterkeys(cm.cmap_d):
cmap = plt.get_cmap(name)
cmap_r = cmap.reversed()
if not cmap_r._isinit:
cmap._init()
cmap_r._init()
assert_array_almost_equal(cmap._lut[:-3], cmap_r._lut[-4::-1])


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
X Tutup