[英]Plotting side-by-side stacked bar charts in plotly from a dataframe
我有以下數據框:
Position Tenure Location Concept
a 0-3 d Y
a 4-7 e N
b 4-7 f M
c 8-11 d M
d 0-3 g Y
b 12-14 f N
我想要的是每個概念的位置、任期和位置的並排堆疊的條形圖。 例如,對於概念 YI,我想要一個位置條形圖(高度為 2,堆疊 a 和 d)旁邊的圖表(高度為 2,在這種情況下只有 0-3)和位置圖表(高度為 2 , 1 代表 d,1 代表 g)。
希望這是有道理的。 謝謝!
聽起來你想要一個分組和堆疊的條形圖,據我所知, 目前不可用。 但是當涉及到對特定數據框的所有方面進行分組和可視化時,plotly 在px.Bar()
的參數中還有其他靈活的選項:
陰謀:
代碼:
# imports
import plotly.express as px
import pandas as pd
# data input
df = pd.DataFrame({'Position': {0: 'a', 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'b'},
'Tenure': {0: '0-3', 1: '4-7', 2: '4-7', 3: '8-11', 4: '0-3', 5: '12-14'},
'Location': {0: 'd', 1: 'e', 2: 'f', 3: 'd', 4: 'g', 5: 'f'},
'Concept': {0: 'Y', 1: 'N', 2: 'M', 3: 'M', 4: 'Y', 5: 'N'}})
# plotly express
fig = px.bar(df, x="Concept", y="Tenure", color="Tenure", barmode="group",
facet_col="Location",color_discrete_sequence=px.colors.diverging.Spectral[-2::-1])
# Remove duplicate x-axis names:
for axis in fig.layout:
if type(fig.layout[axis]) == go.layout.XAxis:
fig.layout[axis].title.text = ''
fig['layout']['xaxis2']['title']['text']='Concept'
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.