I am trying to plot a stacked bar chart using bokeh by following this segment of the documentation. but my data frame is a tad more complex. it looks like this:
events count Name
a 2 jerry
b 1 jerry
a 8 joe
c 1 joe
b 4 megan
c 1 megan
... ... ...
data.user.nunique()
= 11 (will be in columns) and data.event.nunique()
= 167 (will be the stacked segments for each column note that not every user has raised all unique events)
so according to code from the docs and for the above segment of dataframe:
output_file("stacked.html")
names = data.Name.unique() # ['jerry','joe','megan']
events = data.events.unique() # ['a','b','c']
colors =["#c9d9d3", "#718dbf", "#e84d60"]
data = {'names' : names,
'a' : [2, 8, 0], # a raised 2 times by jerry, 8 times by joe , 0 times by megan
'b' : [1, 0, 4],
'c' : [0, 1, 1]}
my question is twofold, 1) how do I create the data
dictionary from my actual dataset?
2) is there any alternative approach to solving this problem?
bokeh doesn't necessarily need a dictionary to work, so we can actually just use the
pivot
Dataframe method to achieve the desired transformation and plot the result directly.Transform the data:
Plot the data:
An alternative way of plotting this is to use the holoviews library (simply adding this because holoviews can produce some waaay more concise code than bokeh). Holoviews takes care of the data transformations for you so you don't need any added effort:
As for alternative solutions, I'm not entirely sure. I can't see visual comparisons being very easy with 167 types of events (that's 167 unique colors, so the colors may not be extremely discernable- not to mention an unwieldly legend with 167 entries). If this way of visualizing doesn't help, I would recommend using the Holoviews library to create a barplot for each of your names. Then you can toggle through a plot for each individual you have in the data.