I am trying to get two stacked bar charts side by side but cannot figure it out.
Here's an example df:
Field Issue
Police Budget cuts
Research Budget cuts
Police Time consuming
Banking Lack of oversight
Healthcare Lack of support
Research Bureaucracy
Healthcare Bureaucracy
Banking Mistrust
What I want is a stacked bar chart of of field first. It will have a height of 8 broken down by 2 police, 2 x research etc. Then I want a stacked bar chart of Issue next to the first chart. This second one will have a height of 8 and be stacked by 2 x budget cuts, 1 x time consuming, 1 x lack of oversight etc.
I have tried:
to get the stacked bar chart of all fields:
trace1 = go.Bar(
x = df.Field.unique(),
y = df.Field.value_counts(),
name='Total Amount of roles'
)
to get a stacked bar chart of Budget cuts (then replicate for other issues):
trace2 = go.Bar(
x = df.Field.unique(),
y = df[df['Issue'] == 'Budget cuts'].Field.value_counts(),
name='Budget cuts'
)
data = [trace1, trace2]
layout = go.Layout(barmode='stack')
fig = go.Figure(data=data, layout=layout)
py.plot(fig, filename='test.html')
But the above code stacks the two graphs onto one. I want trace 1 stacked and trace 2 stacked. I also want this integrated into Dash and not plotly on it's own but that would be secondary to be honest. Would appreciate any help!
EDIT - after a short dialouge in the comments, this is my latest suggestion:
Here's a possible solution with the count of each ocurence of each category stacked per column ( Field or Issue):
Plot:
Code:
As you can see it's not very flexible since you'll have to add one
go.Bar
object for each category (Banking, Police etc). But if the plot above is what you're looking for, I'll sort out that part too.