Python Plotly:如何从数据框创建垂直堆叠条形图?
我有以下数据框“df_百分比”:Python Plotly:如何从数据框创建垂直堆叠条形图?,python,pandas,plotly,Python,Pandas,Plotly,我有以下数据框“df_百分比”: df_percentages Percentages_center Percentages zone2 Percentages total Sleeping 77.496214 87.551742 12.202591 Low activity 21.339391 12.286724 81.51
df_percentages
Percentages_center Percentages zone2 Percentages total
Sleeping 77.496214 87.551742 12.202591
Low activity 21.339391 12.286724 81.511021
Middle activity 0.969207 0.124516 5.226317
High activity 0.158169 0.000000 1.009591
我试图创建一个垂直堆叠的条形图,x轴上有3个独立的条形图:一个用于“百分比\中心”,一个用于“百分比区域2”,一个用于“百分比总数”。1条表示睡眠、低活动、中等活动和高活动的百分比
我使用以下代码尝试了这一点,但我不知道如何制作条形图:
x = ['Center', 'Zone2', 'Total']
plot = px.Figure(data=[go.Bar(
name = 'Sleeping (0-150 MP)',
x = x,
y = df_percentages['Percentages center']
),
go.Bar(
name = 'Low activity (151-2000 MP)',
x = x,
y = df_percentages['Percentages zone2']
),
go.Bar(
name = 'Middle activity (2001-6000 MP)',
x = x,
y = df_percentages['Percentages center']
),
go.Bar(
name = 'High activity (6000-10000)',
x = x,
y = df_percentages['Percentages zone2']
)
])
plot.update_layout(barmode='stack')
plot.show()
如果您对plotly.express开放,我建议使用
df = df.T # to get your df in the right shape
fig = px.bar(df, x = df.index, y = df.columns)
绘图:
完整代码:
如果您对plotly.express开放,我建议使用
df = df.T # to get your df in the right shape
fig = px.bar(df, x = df.index, y = df.columns)
绘图:
完整代码:
我的建议对你有什么影响?我的建议对你有什么影响?