Grabbing individual colors from color maps in matplotlib

Want an individual color from a color map in matplotlib?  You would think this would be easy (and it is as you will see below), but as I traversed the documentation, I could not find for the life of me how to do this.  After looking at various matplotlib functions, Rob and I finally found that it’s really simple (and apparently undocumented as far as we can tell).  Just choose your colormap (e.g. ‘jet’) and call it like a function with a value between 0 and 1: e.g. pylab.cm.jet(.5).  This will return you the rgba 4-tuple that you can use for whatever.

About these ads

4 thoughts on “Grabbing individual colors from color maps in matplotlib

  1. Fantastic, thank you so much!! I’ve been trying to work this out for ages, thinking there must be something simpler than the kind of solutions people suggest. This is great! :)

  2. But the function is taking values beyond 1. What does that mean? For example if I give RdYlGn as my colormap string, blue is also getting included in the tuple. How can I just get red, green and a mixture of red and green (yellow)? How can I get pure red when I give a value of 0 and pure green when giving 1?

    Thanks

  3. If I remember right, it’s kind of a ‘hybrid’ function. Between 0 and 1 you can get the whole spectrum, but then it also works ‘discretely’ to 255 I think. I’m not at a computer now, so I’m not for sure, but I’d be interested to know if that’s it.

    Geet Duggal cs.cmu.edu/~gduggal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s