Range values to pseudocolor
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 colorsys
module (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:
from colorsys import hsv_to_rgb
def pseudocolor(val, minval, maxval):
""" Convert val in range minval..maxval to the range 0..120 degrees which
correspond to the colors Red and Green in the HSV colorspace.
"""
h = (float(val-minval) / (maxval-minval)) * 120
# Convert hsv color (h,1,1) to its rgb equivalent.
# Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
r, g, b = hsv_to_rgb(h/360, 1., 1.)
return r, g, b
if __name__ == '__main__':
steps = 10
print('val R G B')
for val in range(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100)))
Output:
val R G B
0 -> (1.000, 0.000, 0.000)
10 -> (1.000, 0.200, 0.000)
20 -> (1.000, 0.400, 0.000)
30 -> (1.000, 0.600, 0.000)
40 -> (1.000, 0.800, 0.000)
50 -> (1.000, 1.000, 0.000)
60 -> (0.800, 1.000, 0.000)
70 -> (0.600, 1.000, 0.000)
80 -> (0.400, 1.000, 0.000)
90 -> (0.200, 1.000, 0.000)
100 -> (0.000, 1.000, 0.000)
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:
def pseudocolor(val, minval, maxval, start_hue, stop_hue):
""" Convert val in range minval..maxval to the range start_hue..stop_hue
degrees in the HSV colorspace.
"""
h = (float(val-minval) / (maxval-minval)) * (stop_hue-start_hue) + start_hue
# Convert hsv color (h,1,1) to its rgb equivalent.
# Note: hsv_to_rgb() function expects h to be in the range 0..1 not 0..360
r, g, b = hsv_to_rgb(h/360, 1., 1.)
return r, g, b
if __name__ == '__main__':
# angles of common colors in hsv colorspace
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA = range(0, 360, 60)
steps = 10
print('val R G B')
for val in range(0, 100+steps, steps):
print('{:3d} -> ({:.3f}, {:.3f}, {:.3f})'.format(
val, *pseudocolor(val, 0, 100, YELLOW, BLUE)))
Results:
Color Range Python
Easy. On the 0 - 1 scale that colorsys
uses, the hue of red is 0 and the hue of green is 1/3, so you just need to reduce your numbers in the [0 - 100] range to the [0 - 1/3] interval by dividing.
Like this:
import colorsys
for i in range(101):
rgb = colorsys.hsv_to_rgb(i / 300., 1.0, 1.0)
print(i, [round(255*x) for x in rgb])
output
0 [255, 0, 0]
1 [255, 5, 0]
2 [255, 10, 0]
3 [255, 15, 0]
4 [255, 20, 0]
5 [255, 25, 0]
6 [255, 31, 0]
7 [255, 36, 0]
8 [255, 41, 0]
9 [255, 46, 0]
10 [255, 51, 0]
11 [255, 56, 0]
12 [255, 61, 0]
13 [255, 66, 0]
14 [255, 71, 0]
15 [255, 77, 0]
16 [255, 82, 0]
17 [255, 87, 0]
18 [255, 92, 0]
19 [255, 97, 0]
20 [255, 102, 0]
21 [255, 107, 0]
22 [255, 112, 0]
23 [255, 117, 0]
24 [255, 122, 0]
25 [255, 128, 0]
26 [255, 133, 0]
27 [255, 138, 0]
28 [255, 143, 0]
29 [255, 148, 0]
30 [255, 153, 0]
31 [255, 158, 0]
32 [255, 163, 0]
33 [255, 168, 0]
34 [255, 173, 0]
35 [255, 178, 0]
36 [255, 184, 0]
37 [255, 189, 0]
38 [255, 194, 0]
39 [255, 199, 0]
40 [255, 204, 0]
41 [255, 209, 0]
42 [255, 214, 0]
43 [255, 219, 0]
44 [255, 224, 0]
45 [255, 229, 0]
46 [255, 235, 0]
47 [255, 240, 0]
48 [255, 245, 0]
49 [255, 250, 0]
50 [255, 255, 0]
51 [250, 255, 0]
52 [245, 255, 0]
53 [240, 255, 0]
54 [235, 255, 0]
55 [230, 255, 0]
56 [224, 255, 0]
57 [219, 255, 0]
58 [214, 255, 0]
59 [209, 255, 0]
60 [204, 255, 0]
61 [199, 255, 0]
62 [194, 255, 0]
63 [189, 255, 0]
64 [184, 255, 0]
65 [178, 255, 0]
66 [173, 255, 0]
67 [168, 255, 0]
68 [163, 255, 0]
69 [158, 255, 0]
70 [153, 255, 0]
71 [148, 255, 0]
72 [143, 255, 0]
73 [138, 255, 0]
74 [133, 255, 0]
75 [128, 255, 0]
76 [122, 255, 0]
77 [117, 255, 0]
78 [112, 255, 0]
79 [107, 255, 0]
80 [102, 255, 0]
81 [97, 255, 0]
82 [92, 255, 0]
83 [87, 255, 0]
84 [82, 255, 0]
85 [77, 255, 0]
86 [71, 255, 0]
87 [66, 255, 0]
88 [61, 255, 0]
89 [56, 255, 0]
90 [51, 255, 0]
91 [46, 255, 0]
92 [41, 255, 0]
93 [36, 255, 0]
94 [31, 255, 0]
95 [26, 255, 0]
96 [20, 255, 0]
97 [15, 255, 0]
98 [10, 255, 0]
99 [5, 255, 0]
100 [0, 255, 0]
false-/pseudo color coding for temperature
The HSV colourspace might be helpful here.
Hue is periodic, however,, which is to say if you go from red all the way to the other end of the spectrum, you'll be back at red, which isn't that useful in your case. What you'll probably want to do is choose a subset of the hue spectrum that goes from red, through yellow and green, to blue (omitting pink/purple). There's an image in the RGB-HSV section that should show you what I mean.
Update: In fact, this previous answer tells you how to implement exactly the MATLAB Jet palette that you describe.
It seems that Jet is actually a variation on the HSV colourspace anyway!
Calculate RGB value for a range of values to create heat map
def rgb(minimum, maximum, value):
minimum, maximum = float(minimum), float(maximum)
ratio = 2 * (value-minimum) / (maximum - minimum)
b = int(max(0, 255*(1 - ratio)))
r = int(max(0, 255*(ratio - 1)))
g = 255 - b - r
return r, g, b
I need a function to return RGBA values based on a variable
You can use a color representation like HSV to do this. You can take your rgb color max (255, 0, 0) and min (0, 255, 0) and convert those to HSV (0, 100%, 100%) (120, 100%, 100%). Looking at these you can see that only one value changes, the hue. So you can map your range -10:10 to the range of the hue 0:120. This can be done using a pretty simple mapping function:
def map_values(oldValue):
# this is your range -10:10
OldRange = (10 - -10)
# range we are mapping to 0:120
NewRange = (120 - 0)
return (((oldValue - -10) * NewRange) / OldRange) + 0
Now that you have a hue, you can convert that into an RGB value. There is a handy function for this in colorsys
import colorsys
# we need to divide by 360 because this function
# expects a value from 0.0 to 1.0. H has a range from 0 to 360
my_rgb_color = colorsys.hsv_to_rgb(map_values(your_value) / 360.0, 1.0, 1.0)
Python: convert values between 0 and 1 to color range red to blue
Scale your 0-1 input to 0-(-120) output, take the modulus by 360, and use that as your hue (or 0-(-0.3333...) and 1.0 as appropriate).
How to make a color progression out of a color palette
Convert your RGB coordinates to HSL or HSV and step through them, converting back to RGB along the way.
Related Topics
Conda Command Is Not Recognized on Windows 10
Writing to Existing Workbook Using Xlwt
How to Convert a Timezone Aware String to Datetime in Python Without Dateutil
Python Regex Instantly Replace Groups
Capture Arbitrary Path in Flask Route
I Expect 'True' But Get 'None'
Differencebetween Using Loc and Using Just Square Brackets to Filter for Columns in Pandas/Python
How to Enumerate an Object's Properties in Python
How to Generate Circular Thumbnails with Pil
How to Count the Nan Values in a Column in Pandas Dataframe
Too Many Different Python Versions on My System and Causing Problems
Pandas Create New Column with Count from Groupby
Getting Only Element from a Single-Element List in Python
Running a Process in Pythonw with Popen Without a Console
Subprocess.Call() Arguments Ignored When Using Shell=True W/ List