Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created June 2, 2017 05:42
Show Gist options
  • Save cocodrips/c8b1dd90652be6e758dfe99a21b0c371 to your computer and use it in GitHub Desktop.
Save cocodrips/c8b1dd90652be6e758dfe99a21b0c371 to your computer and use it in GitHub Desktop.
import matplotlib
def bg_color(value, min_, max_, center_=None):
min_ = float(min_)
max_ = float(max_)
if center_ is None:
center_ = (max_ - min_) / 2
cmap = matplotlib.cm.get_cmap('coolwarm')
vv = 0
if value < center_:
v = (center_ - value) / (center_ - min_)
vv = 0.5 - (0.5 * v)
else:
v = (value - center_) / (max_ - center_)
vv = 0.5 + (0.5 * v)
if vv > 1:
return 'background-color: {}'.format('#111111')
return 'background-color: {}'.format(matplotlib.colors.to_hex(cmap(vv)))
print(bg_color(-0.5, -1, 1, 0))
print(bg_color(0, -1, 1, 0))
print(bg_color(1, -1, 1, 0))
#8db0fe
#dddcdc
#b40426
df.corr().style.applymap(lambda x: bg_color(x, -1, 0.999, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment