[英]How to plot a horizontal Stacked bar plot using Plotly-Python?
[英]stacked bar plot in plotly in one column python
我有這個數據框:
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%
我努力了:
fig = px.bar(df, x="percentage", color='rules', orientation='h',
height=400,
title='rules')
fig.show()
但結果不一樣。
為了獲得預期的輸出,y 軸必須准備好唯一的數據。 由於百分比是字符串,我們需要將權重字符串除以 100 以便稍后顯示百分比。 圖表上的調整是添加一系列條形。 我們還將百分比和 x 軸刻度線更改為百分比。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.