Last active
December 14, 2015 10:10
-
-
Save dettmering/5070274 to your computer and use it in GitHub Desktop.
Python: Generate heat map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rgb(r,g,b): # Just returns r, g, b in this case. Can be used to reformat RGB values if necessary. | |
return r,g,b | |
def heat(min, max, val, type): # Takes min and max values and the value to be heatmapped. Type is the look up table, see below. | |
min = float(min) | |
max = float(max) | |
val = float(val) | |
val = (val - min) / (max - min) # normalize val to min and max | |
r = float(0) | |
g = float(0) | |
b = float(0) | |
if type == 'grey': | |
r = val | |
g = val | |
b = val | |
if type == 'greyinv': | |
r = 1 - val | |
g = 1 - val | |
b = 1 - val | |
if type == 'heat': # green --> red | |
if val <= 0.5: | |
g = 1 | |
r = val / 0.5 | |
b = 0 | |
if val > 0.5: | |
r = 1 | |
g = 2 + (val / (-(1 - 0.5))) | |
b = 0 | |
elif type == 'greenbow': # weird, but beautiful. Don't use for science. | |
if val <= (1.0 / 3.0): | |
r = 0 | |
g = val / (1.0 / 3.0) | |
b = 1.0 - g | |
if val > (1.0 / 3.0) and val <= (2.0 / 3.0): | |
g = 1.0 | |
b = 0 | |
r = (val - (1.0 / 3.0)) / (1.0 / 3.0) | |
if val > (2.0 / 3.0): | |
g = 1.0 - (val - (2.0 / 3.0)) / (1.0 / 3.0) | |
r = 1.0 | |
b = 0 | |
elif type == 'rainbow': # rainbow colors | |
if val < 0: | |
r = 0 | |
g = 0 | |
b = 1 | |
if val >= 0 and val <= 0.25: | |
r = 0 | |
g = val / 0.25 | |
b = 1 | |
if val > 0.25 and val <= 0.5: | |
b = 1 - (val - 0.25) / 0.25 | |
g = 1 | |
r = 0 | |
if val > 0.5 and val <= 0.75: | |
g = 1 | |
r = (val - 0.5) / 0.25 | |
b = 0 | |
if val > 0.75: | |
r = 1 | |
g = 1 - ( (val - 0.75) / 0.25 ) | |
b = 0 | |
colours = rgb(r,g,b) | |
return colours # colours are returned as a fraction of 1. 1,1,1 = white; 0,0,0 = black. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment