# how can i plot a stacked bar chart by year with two values(columns)?

not sure if this is correct medium, but i am having trouble plotting this simple data:

how do i get the graph below - aplogies for this poor picture.

basically i want it stacked with the risk label being the legend, but the predicted and actual side by side for each year.. how can i achieve this in pandas? btw this is a pandas dataframe

e.g.

df_partial = df[['year','risk','predicted','actual']]
grouped = df_partial.groupby(['year','risk']).sum()[['predicted','actual']]
grouped.plot(kind='bar')


it does not give me what i would like to see

import plotly.graph_objs as go
df = df.melt(id_vars=["year", "risk"], value_vars=["predicted", "actual"])
fig = go.Figure()

for risk in ["low", "medium", "high"]:
tmp_df = df.query(f"risk == '{risk}'")
go.Bar(
x=[tmp_df["year"], tmp_df["variable"]], y=tmp_df["value"], name=risk,
)
)

fig.update_layout(barmode="stack", height=400, width=800, margin=dict(b=60, t=20, l=20, r=20))
fig.show()


however i get :

how do i remove these faint lines? i've tried opacity does now work

• Have a look at plotly
– null
Jun 25 '20 at 20:59
• @null how can i do thid using plotly - i have some code above using it but i want a bar chart for predicted side by side each year Jun 26 '20 at 8:07
• You can try this question on StackOverflow. I believe you will get a quick and better answer Jun 26 '20 at 13:10