Reputation: 330
I have a dataframe (mD
) like this:
Pr Dt Cd Cn Rn
GT 2017-01-12 60 1 'ZP0'
GT 2017-01-12 60 6 'ZP5'
GT 2017-01-12 61 0 'ZP0'
GT 2017-01-12 61 7 'ZP6'
GT 2017-01-12 65 7 'ZP4'
GT 2017-01-10 63 1 'ZP4'
GT 2017-01-10 65 2 'ZP4'
GT 2017-01-10 68 3 'ZP2'
GT 2017-01-09 62 8 'ZP1'
GT 2017-01-12 62 1 'ZP1'
GT 2017-01-11 62 2 'ZP0'
GT 2017-01-11 60 2 'ZP0'
GT 2017-01-10 66 4 'ZP5'
GT 2017-01-10 60 1 'ZP6'
GT 2017-01-09 68 1 'ZP2'
GT 2017-01-09 65 1 'ZP0'
GT 2017-01-09 62 1 'ZP3'
GT 2017-01-09 62 1 'ZP3'
BW 2017-01-11 61 0 'ZP0'
BW 2017-01-10 61 1 'ZP1'
BW 2017-01-10 61 6 'ZP0'
BW 2017-01-11 61 0 'ZP5'
BW 2017-01-11 62 1 'ZP5'
BW 2017-01-12 62 6 'ZP7'
BW 2017-01-11 60 5 'ZP0'
BW 2017-01-09 66 4 'ZP2'
And i want to make a HeatMap in Bokeh, in which x = Cd
(which goes from 60 to 70), y = Dt
(which has a datetime format) and values = Cn
. In words, i want a heatmap for each product (Pr
) in which is plotted the codes (Cd
) versus the date (Dt
) and the color has to represent the sum of the counts (Cn
) for each date and code.
My first attempt was like this (after importing the libraries of course):
mHt = HeatMap(mD, x = 'Cd', y = 'Dt', values = 'Cn')
output_file("Heatmap.html", title="Heatmap")
show(mHt)
However, this results in a blank canvas. I've tried to order my data with .groupby
, pd.pivot_table
and pd.crosstab
, but i've failed so far. I'm new with Bokeh, so, anyone has any suggestions?
Upvotes: 2
Views: 940
Reputation: 3364
Edit:The bokeh charts repo is no longer maintained, so if you want to use the most recent version of bokeh, you should use Holoviews, or write your own code to make the heatmap.
Try the below code, seems to work.
Note: If you wanted a separate heat map for each color, just filter the df and create separate plots.
Alternatively create a widget that lets you pick which set of data you wish to visualize.
import pandas as pd
from bokeh.charts import HeatMap, bins
from bokeh.plotting import figure, output_file, show,ColumnDataSource
from bokeh.models import DatetimeTickFormatter
Dt = ['2017-01-12','2017-01-12','2017-01-12','2017-01-12','2017-01-12',
'2017-01-10','2017-01-10','2017-01-10','2017-01-09','2017-01-12',
'2017-01-11','2017-01-11','2017-01-10','2017-01-10','2017-01-09',
'2017-01-09','2017-01-09','2017-01-09','2017-01-11','2017-01-10',
'2017-01-10','2017-01-11','2017-01-11','2017-01-12','2017-01-11',
'2017-01-09']
Pr = ['GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT','GT',
'GT','GT','GT','GT','BW','BW','BW','BW','BW','BW', 'BW', 'BW']
Cd = [60,60,61,61,65,63,65,68,62,62,62,60,66,60,68,65,62,62,61,61,61,61,62,62,60,66]
Cn = [1,6,0,7,7,1,2,3,8,1,2,2,4,1,1,1,1,1,0,1,6,0,1,6,5,4]
df = pd.DataFrame({'Pr':Pr,'Dt':Dt,'Cd':Cd,'Cn':Cn})
datadict = df.to_dict(orient='list')
source = ColumnDataSource(datadict)
h1 = HeatMap(source.data, x=bins('Cd'), y='Dt', values='Cn')
show(h1)
Upvotes: 2