Reputation: 880
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.
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.
Upvotes: 1
Views: 830
Reputation: 35230
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()
Upvotes: 1