Husni
Husni

Reputation: 35

Concatenate Bokeh Stacked Bar Plots to visualise changes

I have two dataframes

df1 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],['1.2','3.5','44','77','3.4','24','11','12','13'], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T

df.columns = [['QID','score', 'DocID']]

df2 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],['21.2','13.5','12','77.6','3.9','29','17','41','32'], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T

df.columns = [['QID','score', 'DocID']]

Currently, I'm plotting scores using bokeh in df1 and df2 in two different graphs as

df1_BarDocID = Bar(df1, 'QID', values='score', stack = 'DocID', title="D1: QID Stacked by DocID on Score")

D2_BarDocID = Bar(df2, 'QID', values='score', stack = 'DocID', title="D1: QID Stacked by DocID on Score")

grid = gridplot([[D1_BarDocID, D2_BarDocID]])
show(grid)

But, I want to plot two Dataframes in a single figure in a way that the outputs of Df1 and Df2 are plotted side by side for a single QID. So I can visualise the difference in score between two DataFrames, using bokeh.

df1 & df2 plots, using bokeh

enter image description here

Upvotes: 0

Views: 1461

Answers (1)

bigreddot
bigreddot

Reputation: 34568

Here is a complete example using the newer vbar_stack and stable bokeh.plotting API. It could probably be made simpler but my Pandas knowledge is limited:

import pandas as pd

from bokeh.core.properties import value
from bokeh.io import output_file
from bokeh.models import FactorRange
from bokeh.palettes import Spectral8
from bokeh.plotting import figure, show

df1 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],[1.2, 3.5, 44, 77, 3.4, 24, 11, 12, 13], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T
df1.columns = ['QID','score', 'DocID']
df1 = df1.pivot(index='QID', columns='DocID', values='score').fillna(0)
df1.index = [(x, 'df1') for x in df1.index]

df2 = pd.DataFrame([['1','1','1','2','2','2','3','3','3'],[21.2, 13.5, 12, 77.6, 3.9, 29, 17, 41, 32], ['30312', '20021', '23423', '23424', '45646', '34535', '35345', '34535', '76786']]).T
df2.columns = ['QID','score', 'DocID']
df2 = df2.pivot(index='QID', columns='DocID', values='score').fillna(0)
df2.index = [(x,'df2') for x in df2.index]

df = pd.concat([df1, df2])

p = figure(plot_width=800, x_range=FactorRange(*df.index))
p.vbar_stack(df.columns, x='index', width=0.8, fill_color=Spectral8, 
             line_color=None, source=df, legend=[value(x) for x in df.columns]) 

p.legend.location = "top_left"

output_file('foo.html')
show(p)

Produces:

enter image description here

Upvotes: 2

Related Questions