# Reverse colormap in matplotlib

I would like to know how to simply reverse the color order of a given colormap in order to use it with plot_surface.

## Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

### Solution 1

The standard colormaps also all have reversed versions. They have the same names with `_r` tacked on to the end. (Documentation here.)

### Solution 2

The solution is pretty straightforward. Suppose you want to use the “autumn” colormap scheme. The standard version:

``````cmap = matplotlib.cm.autumn
``````

To reverse the colormap color spectrum, use get_cmap() function and append ‘_r’ to the colormap title like this:

``````cmap_reversed = matplotlib.cm.get_cmap('autumn_r')
``````

### Solution 3

In matplotlib a color map isn’t a list, but it contains the list of its colors as `colormap.colors`. And the module `matplotlib.colors` provides a function `ListedColormap()` to generate a color map from a list. So you can reverse any color map by doing

``````colormap_r = ListedColormap(colormap.colors[::-1])
``````

### Solution 4

As of Matplotlib 2.0, there is a `reversed()` method for `ListedColormap` and `LinearSegmentedColorMap` objects, so you can just do

```cmap_reversed = cmap.reversed() ```

Here is the documentation.

### Solution 5

As a `LinearSegmentedColormaps` is based on a dictionary of red, green and blue, it’s necessary to reverse each item:

``````import matplotlib.pyplot as plt
import matplotlib as mpl
def reverse_colourmap(cmap, name = 'my_cmap_r'):
"""
In:
cmap, name
Out:
my_cmap_r

Explanation:
t goes from 0 to 1
row i:   x  y0  y1 -> t t t
/
/
row i+1: x  y0  y1 -> t[n] t t

so the inverse should do the same:
row i+1: x  y1  y0 -> 1-t t t
/
/
row i:   x  y1  y0 -> 1-t[n] t t
"""
reverse = []
k = []

for key in cmap._segmentdata:
k.append(key)
channel = cmap._segmentdata[key]
data = []

for t in channel:
data.append((1-t,t,t))
reverse.append(sorted(data))

LinearL = dict(zip(k,reverse))
my_cmap_r = mpl.colors.LinearSegmentedColormap(name, LinearL)
return my_cmap_r
``````

See that it works:

``````my_cmap
<matplotlib.colors.LinearSegmentedColormap at 0xd5a0518>

my_cmap_r = reverse_colourmap(my_cmap)

fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = my_cmap, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = my_cmap_r, norm=norm, orientation='horizontal')
`````` EDIT

I don’t get the comment of user3445587. It works fine on the rainbow colormap:

``````cmap = mpl.cm.jet
cmap_r = reverse_colourmap(cmap)

fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = cmap, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = cmap_r, norm=norm, orientation='horizontal')
`````` But it especially works nice for custom declared colormaps, as there is not a default `_r` for custom declared colormaps. Following example taken from http://matplotlib.org/examples/pylab_examples/custom_cmap.html:

``````cdict1 = {'red':   ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.1),
(1.0, 1.0, 1.0)),

'green': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),

'blue':  ((0.0, 0.0, 1.0),
(0.5, 0.1, 0.0),
(1.0, 0.0, 0.0))
}

blue_red1 = mpl.colors.LinearSegmentedColormap('BlueRed1', cdict1)
blue_red1_r = reverse_colourmap(blue_red1)

fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])

norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap = blue_red1, norm=norm,orientation='horizontal')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap = blue_red1_r, norm=norm, orientation='horizontal')
`````` ### Solution 6

There is no built-in way (yet) of reversing arbitrary colormaps, but one simple solution is to actually not modify the colorbar but to create an inverting Normalize object:

``````from matplotlib.colors import Normalize

class InvertedNormalize(Normalize):
def __call__(self, *args, **kwargs):
return 1 - super(InvertedNormalize, self).__call__(*args, **kwargs)
``````

You can then use this with `plot_surface` and other Matplotlib plotting functions by doing e.g.

``````inverted_norm = InvertedNormalize(vmin=10, vmax=100)
ax.plot_surface(..., cmap=<your colormap>, norm=inverted_norm)
``````

This will work with any Matplotlib colormap.

### Solution 7

There are two types of LinearSegmentedColormaps. In some, the _segmentdata is given explicitly, e.g., for jet:

``````>>> cm.jet._segmentdata
{'blue': ((0.0, 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65, 0, 0), (1, 0, 0)), 'red': ((0.0, 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89, 1, 1), (1, 0.5, 0.5)), 'green': ((0.0, 0, 0), (0.125, 0, 0), (0.375, 1, 1), (0.64, 1, 1), (0.91, 0, 0), (1, 0, 0))}
``````

For rainbow, _segmentdata is given as follows:

``````>>> cm.rainbow._segmentdata
{'blue': <function <lambda> at 0x7fac32ac2b70>, 'red': <function <lambda> at 0x7fac32ac7840>, 'green': <function <lambda> at 0x7fac32ac2d08>}
``````

We can find the functions in the source of matplotlib, where they are given as

``````_rainbow_data = {
'red': gfunc,   # 33: lambda x: np.abs(2 * x - 0.5),
'green': gfunc, # 13: lambda x: np.sin(x * np.pi),
'blue': gfunc,  # 10: lambda x: np.cos(x * np.pi / 2)
}
``````

Everything you want is already done in matplotlib, just call cm.revcmap, which reverses both types of segmentdata, so

``````cm.revcmap(cm.rainbow._segmentdata)
``````

should do the job – you can simply create a new LinearSegmentData from that. In revcmap, the reversal of function based SegmentData is done with

``````def _reverser(f):
def freversed(x):
return f(1 - x)
return freversed
``````

while the other lists are reversed as usual

``````valnew = [(1.0 - x, y1, y0) for x, y0, y1 in reversed(val)]
``````

So actually the whole thing you want, is

``````def reverse_colourmap(cmap, name = 'my_cmap_r'):
return mpl.colors.LinearSegmentedColormap(name, cm.revcmap(cmap._segmentdata))
``````

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0