0

I am trying to make a grouped bar chart in plotly python. But I have not been able to get it to work. It has drawn me the general groups (API1, API2, API3) but it draws me the accumulated bars and I want differentiated bars for each of the S1, S2 and S3

API1 API2 API3
s1 41 56 48
s2 40 50 45
s3 15 24 8

This is the output I have, but I can't get individual bars to be created for each of the groups.

enter image description here

2
  • post your dataframe as a dict please, so we can recreate your issue. Commented Jul 27, 2022 at 13:00
  • Try the following code. import plotly.express as px;df = df.T;px.bar(df, x=df.index, y=['s1','s2','s3'], barmode='stack') Commented Jul 27, 2022 at 13:00

1 Answer 1

2
df.reset_index(inplace=True)
df.rename(columns={'index': 'group'}, inplace=True)
df
##
  group  API1  API2  API3
0    s1    41    56    48
1    s2    40    50    45
2    s3    15    24     8
df_plot = df.melt(value_vars=df.columns, id_vars='group')
df_plot
###
  group variable  value
0    s1     API1     41
1    s2     API1     40
2    s3     API1     15
3    s1     API2     56
4    s2     API2     50
5    s3     API2     24
6    s1     API3     48
7    s2     API3     45
8    s3     API3      8



Plot

fig = px.histogram(df_plot, x='variable', y='value', color='index', barmode='group')
fig.show()

enter image description here

fig2 = px.histogram(df_plot, x='variable', y='value', color='index')
fig2.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.