8000 A method added to Colormap classes to reverse the colormap by kjartankg · Pull Request #6211 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

A method added to Colormap classes to reverse the colormap #6211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
A method added to Colormap classes to reverse the colormap
* method added to both ListedColormap and LinearSegmentedColormap
* The parent class Colormap raises NotImplementedError
* A test was added to test_colors.py
  • Loading branch information
kjartankg committed Mar 23, 2016
commit 9ceb6b91ab4530e2da0015860124e712eae6aeaa
79 changes: 79 additions & 0 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,30 @@ 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use .. note :: and sphinx will render this better.


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".

Raises
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this section.

------
NotImplementedError
Function not implemented for base class.

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 +808,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 +914,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
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ def test_pandas_iterable():
cm2 = mcolors.ListedColormap(s, N=5)
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
Expand Down
0