1

I have this dataframe:

rules weight percentage
r1     40      40%
r2     20      20%
r3     10      10%
r4     10      10%
r5     10      10%
r6     5        5%
r7     5        5%
r8     0        0%
r9     0        0%

and I want to create a stacked bar chart in plotly, but in one column. A plot of this kind. enter image description here

I have tried:

fig = px.bar(df, x="percentage", color='rules', orientation='h',
             height=400,
             title='rules')
fig.show()

but the result is not the same.

The results looks like this graph, the numbers aren't the same, because I have created a toy example for this question. enter image description here

1
  • Please show how your current result looks like Commented Jul 18, 2022 at 19:33

1 Answer 1

1

To get the expected output, the y-axis must be prepared with unique data. And since the percentages are strings, we need to divide the weight string by 100 in order to display the percentages later. The adjustment on the graph is adding a range of bars. We are also changing the percentages and the x-axis tick marks to percentages.

df['item'] = 'rule'
df['weight'] = df['weight'] / 100

import plotly.express as px
fig = px.bar(df, x="weight", y='item', color='rules', orientation='h', barmode='stack', text_auto='.0%',
             height=400,
             title='rules')
fig.update_xaxes(range=[0,1.0], tickvals=np.arange(0,1.1,0.2), ticktext=[str(x)+'%' for x in np.arange(0,101,20)])

fig.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.