簡體   English   中英

一列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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM