Reverse colormap in matplotlib


Question

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

1
202
7/23/2018 2:31:19 PM

Accepted Answer

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

384
11/1/2014 5:13:13 AM

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])

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon