Luis Miguel
Luis Miguel

Reputation: 5137

Reversing colormaps or specifying colors in a matplotlib/pandas plot

I am following some examples from the book Python for Data Analysis, and came across an interesting problem. I was wondering if there is a way to "reverse" of a colormap, for example from a blue extreme to a pink extreme to the opposite (pink to blue) for the 'cool' color map.

SciPy.org colormaps

In the example below, I chart baby names using the 'cool' color map, that shows girls in blue color and boy in pink color. Is there a way to 'reverse' the mapping?

enter image description here

Upvotes: 2

Views: 1802

Answers (1)

mgilson
mgilson

Reputation: 310267

It's not well documented in the link you showed, but each colormap has a corresponding reversed colormap that you can access by appending an "_r" to the colormap name. In your case, the colormap name would be "cool_r".

This fact is hinted at in the code samples (look for the .endswith("_r") portions of the code), but it isn't really stated very succinctly.

Upvotes: 3

Related Questions