8000 Merge pull request #6211 from kjartankg/reverse-cm · matplotlib/matplotlib@6f27f95 · GitHub
[go: up one dir, main page]

Skip to content

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Colormap reversed method
2+
------------------------
3+
4+
The methods :meth:`~matplotlib.colors.LinearSegmentedColormap.reversed` and
5+
:meth:`~matplotlib.colors.ListedColormap.reversed` return a reversed
6+
instance of the Colormap. This implements a way for any Colormap to be
7+
reversed.

lib/matplotlib/colors.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,25 @@ def _resample(self, lutsize):
665665
"""
666666
raise NotImplementedError()
667667

668+
def reversed(self, name=None):
669+
"""
670+
Make a reversed instance of the Colormap.
671+
672+
.. note :: Function not implemented for base class.
673+
674+
Parameters
675+
----------
676+
name : str, optional
677+
The name for the reversed colormap. If it's None the
678+
name will be the name of the parent colormap + "_r".
679+
680+
Notes
681+
-----
682+
See :meth:`LinearSegmentedColormap.reversed` and
683+
:meth:`ListedColormap.reversed`
684+
"""
685+
raise NotImplementedError()
686+
668687

669688
class LinearSegmentedColormap(Colormap):
670689
"""Colormap objects based on lookup tables using linear segments.
@@ -784,6 +803,40 @@ def _resample(self, lutsize):
784803
"""
785804
return LinearSegmentedColormap(self.name, self._segmentdata, lutsize)
786805

806+
def reversed(self, name=None):
807+
"""
808+
Make a reversed instance of the Colormap.
809+
810+
Parameters
811+
----------
812+
name : str, optional
813+
The name for the reversed colormap. If it's None the
814+
name will be the name of the parent colormap + "_r".
815+
816+
Returns
817+
-------
818+
LinearSegmentedColormap
819+
The reversed colormap.
820+
"""
821+
if name is None:
822+
name = self.name + "_r"
823+
824+
# Function factory needed to deal with 'late binding' issue.
825+
def factory(dat):
826+
def func_r(x):
827+
return dat(1.0 - x)
828+
return func_r
829+
830+
data_r = dict()
831+
for key, data in six.iteritems(self._segmentdata):
832+
if six.callable(data):
833+
data_r[key] = factory(data)
834+
else:
835+
new_data = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)]
836+
data_r[key] = new_data
837+
838+
return LinearSegmentedColormap(name, data_r, self.N, self._gamma)
839+
787840

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

912+
def reversed(self, name=None):
913+
"""
914+
Make a reversed instance of the Colormap.
915+
916+
Parameters
917+
----------
918+
name : str, optional
919+
The name for the reversed colormap. If it's None the
920+
name will be the name of the parent colormap + "_r".
921+
922+
Returns
923+
-------
924+
ListedColormap
925+
A reversed instance of the colormap.
926+
"""
927+
if name is None:
928+
name = self.name + "_r"
929+
930+
colors_r = list(reversed(self.colors))
931+
return ListedColormap(colors_r, name=name, N=self.N)
932+
859933

860934
class Normalize(object):
861935
"""

lib/matplotlib/tests/test_colors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,18 @@ def test_pandas_iterable():
588588
assert_sequence_equal(cm1.colors, cm2.colors)
589589

590590

591+
def test_colormap_reversing():
592+
"""Check the generated _lut data of a colormap and corresponding
593+
reversed colormap if they are almost the same."""
594+
for name in six.iterkeys(cm.cmap_d):
595+
cmap = plt.get_cmap(name)
596+
cmap_r = cmap.reversed()
597+
if not cmap_r._isinit:
598+
cmap._init()
599+
cmap_r._init()
600+
assert_array_almost_equal(cmap._lut[:-3], cmap_r._lut[-4::-1])
601+
602+
591603
if __name__ == '__main__':
592604
import nose
593605
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0