I have a certain array of floats (in Python) that might range from 0 to 100. I want to create a pseudo-color image so that the colors vary from green (corresponding to 0) to red (100). This is similar to pcolor from matplotlib. However, I do not want to use pcolor.
Is there a function like pseudocolorForValue(val,(minval,maxval)) which returns an RGB triple corresponding to the pseudo-color value for val? Also, is there a flexibility in this function to choose whether to display colors from green-to-red or from red-to-green?
Thanks,
Nik
You could write your own function that converted the values 0…100 → 0…120 degrees and then used that value as the H (or angle) of a color in the HSV (or HLS) colorspace. This could then be converted into an RGB color for display purposes. Linearly interpreted colors often look better when they’re calculated in this colorspace: Here’s what HSV colorspace looks like:
Update:
Good news, I was pleasantly surprised to discover that Python has colorspace conversion routines in its built-in
colorsysmodule (they really mean “batteries included”). What’s nice about that is that it makes creating a function that does what I described fairly easy, as illustrated below:Output:
Here’s a sample showing what its output looks like:
I think you may find the colors generated nicer than in my other answer.
Generalizing:
It’s possible to modify this function to be a little more generic in the sense that it will work with colors other then just the Red and Green currently hardcoded into it.
Here’s how to do that:
Results: