0
$\begingroup$

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

enter image description here enter image description here

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}'")
    fig.add_trace(
        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 :

enter image description here

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

$\endgroup$
3
  • $\begingroup$ Have a look at plotly $\endgroup$
    – null
    Jun 25 '20 at 20:59
  • $\begingroup$ @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 $\endgroup$
    – Maths12
    Jun 26 '20 at 8:07
  • $\begingroup$ You can try this question on StackOverflow. I believe you will get a quick and better answer $\endgroup$
    – 10xAI
    Jun 26 '20 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.