Skip to content

Instantly share code, notes, and snippets.

@visualbuffer
Last active February 19, 2019 10:51
Show Gist options
  • Save visualbuffer/2d816a40c91bbef8e60c9e4c7020fbe1 to your computer and use it in GitHub Desktop.
Save visualbuffer/2d816a40c91bbef8e60c9e4c7020fbe1 to your computer and use it in GitHub Desktop.
Sample code for Heatmap generation : use case emailing visualisation profile
# https://medium.com/@shuvashish.chatterjee/putting-graphs-inside-emails-bb6dfc16553c
import matplotlib.cm as cm
import matplotlib.colors as colors
import pandas as pd
import numpy as np
d = np.random.randint(0,20,size=(8,7))
df = pd.DataFrame(data =d,columns=['Mon','Tue','Wed','Thur','Fri','Sat','Sun'])
def heatmapHtml(df):
cmap = cm.get_cmap('summer')
cmap.set_bad('white',1.)
nrows, ncolumns = df.shape
minimum = np.nanmin(df.values)
maximum = np.nanmax(df.values)
norm = colors.Normalize(vmin=minimum, vmax=maximum)
table=""" <style>
.value{ margin: 0px;display: none;padding: 0px; }
.label{ margin: 0px; padding: 0px; }
.cell:hover>.value{ display: inline-block; }
.table{table-layout:fixed;width:100%; min-width: 400px;height: 50%;min-height: 400px;}
.ylabel{padding: 0px;cursor:pointer;opacity: 0.8;width: 10px;height: 10px;text-align: center;}
</style>
<table class="table" table-layout="fixed" border="0">
<thead>
<tr>
<th width="15%"> <h6 class="label" ></h6></th>"""
for c in range(ncolumns):
table = table + """ <th> <h6 class="label" >"""+str(df.columns[c]) + """</h6></th> """
table = table + """ </tr></thead><tbody> """
for r in range(nrows):
table= table + """<tr> <td class="ylabel" > <h6 class = "label"> """ + str(df.index[r]) + """</h6></td> """
for c in range(ncolumns):
value= df.iloc[r,c]
if np.isnan(value):
hexcode="#ffffff"
# print value, c
else:
hexcode =colors.rgb2hex(cmap(norm( value )))
table=table + """<td style="background:""" + hexcode + """;" class="cell" ><h6 class = "value">"""+ str(value)+ """</h6></td>"""
table = table+ """ </tr> """
table = table + """ </tbody></table> """
hexmin=colors.rgb2hex(cmap(norm( minimum )))
hexmid=colors.rgb2hex(cmap(norm( (minimum + maximum)/2 )))
hexmax=colors.rgb2hex(cmap(norm( maximum )))
mid = str(int(round((minimum + maximum)/2)) )
maximum =str(int(round( maximum)) )
minimum =str(int(round( minimum)) )
full="100%"
quarter="25%"
table=table+ """
<hr>
<table width="%(full)s">
<tr width = "%(full)s" height ="10">
<td style="width: %(quarter)s"> <h6 style="margin:2px;"> Legend</h6></td>
<td style="background: %(hexmin)s; width: %(quarter)s; color:white;"><h6 style="margin:2px;">Minimum %(minimum)s</h6></td>
<td style="background: %(hexmid)s; width: %(quarter)s"><h6 style="margin:2px;">Average %(mid)s</h6></td>
<td style="background: %(hexmax)s; width: %(quarter)s"><h6 style="margin:2px;">Maximum %(maximum)s</h6></td>
</tr>
</table>
""" % { "minimum":minimum , "maximum":maximum , "mid":mid , "hexmin":hexmin , "hexmid":hexmid , "hexmax":hexmax, "quarter" :quarter , "full" :full }
return table
table = heatmapHtml(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment