Hi,
I am trying to make stacked bar plot with unrelated categories in each stack.
All examples I have found are based on using the same categories.
I found example/solution with pivoting the data. While this creates a plot as expected it seems ‘wrong’ to create a sparse matrix for a simple data set. Additionally I have ‘other uses’ for the original datasource in my plot, now I have to provide what is essentially the same data twice.
I couldnt find anyting build-in to handle this. Anyone has a reference to a an example on doing a stacked bar plot from a normalised relational dataset.
Or am I better of creating the individual glyphs myself directly from my dataset (a reference to an example for that would also be appreciated)
I have some additional problems even with pivoting the data. I have tertiary categories (category attributes) that are functions of (attribtues of) the the 2 primary categories AND a secondary value beside the primary value used for the bar plot (bar height), which I would like to have in my dataset and display in a hover tool.
The following example show the essence. First a working simple plot on a simple pivot. Then a more advanced pivot that give a dataframe with the tertiary categories (attributes) and secondary values, but as the dimensions of the dataframe is now arrays I am not sure how to reference them in the plotting.
The second plot doesn’t work - I know why, but how do I achieve what I want.
Achieving it might be easier if I go generate the glyphs myself directly from the dataset (going back to my first point).
A reference for an example of that would be appreciated. I.e. advanced enough to create the glyphs in such a way that it can reference back to the original dataframe with references for the fields needed for hovertool and sizing.
Thanks in advance for any help. I am fairly new with bokeh
from pandas import DataFrame
import bokeh.io
# from bokeh.charts import show
from bokeh.models import DatetimeTickFormatter
from bokeh.models import ColumnDataSource
import bokeh.plotting
from bokeh.plotting import figure
import bokeh.palettes
sales = DataFrame.from_records(
[{'Brand': 'Ford', 'Origin': 'USA', 'Model': 'Mustang', 'Type': 'Car', 'Sales': 4, 'Value': 10000},
{'Brand': 'Ford', 'Origin': 'USA', 'Model': 'Mondeo', 'Type': 'Car', 'Sales': 6, 'Value': 4000},
{'Brand': 'Ford', 'Origin': 'USA', 'Model': 'Transit', 'Type': 'Van', 'Sales': 2, 'Value': 3000},
{'Brand': 'Honda', 'Origin': 'Japan', 'Model': 'Accord', 'Type': 'Car', 'Sales': 7, 'Value': 3500},
{'Brand': 'Honda', 'Origin': 'Japan', 'Model': 'Civic', 'Type': 'Car', 'Sales': 3, 'Value': 1500},
{'Brand': 'Volkswagen', 'Origin': 'Germany', 'Model': 'Beetle', 'Type': 'Car', 'Sales': 2, 'Value': 2000},
{'Brand': 'Volkswagen', 'Origin': 'Germany', 'Model': 'Passat', 'Type': 'Car', 'Sales': 4, 'Value': 2800}
]
)
#sales_piv = sales.pivot(index="Brand", columns="Model", values=["Value", 'Sales']) #.fillna(0)
sales_piv = sales.pivot(index="Brand", columns="Model", values="Sales").fillna(0)
print(sales_piv)
p=figure(x_range=sales_piv.index.tolist(),
height=1000,
title="Sales pr Brand",
tools="hover,tap",
tooltips=[("Brand", "???"),
("Model", "$name"),
("Sales", "@$name{0.00}")]
)
p.vbar_stack(sales_piv.columns.tolist(), x='Brand', width=0.9, color=bokeh.palettes.viridis(sales_piv.columns.size), source=sales_piv)
bokeh.plotting.output_file('sales1.html')
bokeh.plotting.show(p)
#More advanced - does not work
sales_piv = sales.pivot(index=["Brand", "Origin"], columns=["Model", "Type"], values=["Sales", "Value"]).fillna(0)
# Alternatively 'Origin' could be in columns that would also eliminate any issues where different rows attributed same Brand to different Origins!
print(sales_piv)
#Do I need to do something here to 're-index' my sales_piv DF to be indexed on "Brand" alone and having the other index columns as normal attributes?
#Do I need to do something here to 're-column' my sales_piv DF
p=figure(x_range=[x[0] for x in sales_piv.index.tolist()], # this extract the Brand correctly but that will then be usable to lookup for the individual plots
height=1000,
title="Sales pr Brand",
tools="hover,tap",
tooltips=[("Brand", "???"),
("Model", "$name"),
("Origin", "???"),
("Type", "???"),
("Sales", "@$name{0}"),
("Value", "???")]
)
p.vbar_stack([x[1] for x in sales_piv.columns.tolist() if x[0] == 'Sales'], # this exctract the Model correctly for finding the stacking value but it cannot lookup in the dataframe
x='Brand',
width=0.9,
color=bokeh.palettes.viridis(([x[1] for x in sales_piv.columns.tolist() if x[0] == 'Sales']).__len__()),
source=sales_piv)
bokeh.plotting.output_file('sales2.html')
bokeh.plotting.show(p)