有没有一种在python中使用plotly express对堆叠条形图进行分组的方法?

2024-05-15 14:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用plotly express来可视化数据,因为它提供了交互式堆叠条形图功能。目前,我正在根据“总居住类型”绘制数据。有三种住宅类型:总住宅、城市住宅和农村住宅。有人知道我如何在不丢失任何这些特征的情况下,在图表上对这些住宅类型进行分组吗

这是我当前的代码:

```import plotly.express as px

fig = px.bar(jmp_df[jmp_df['Residence Type']=='total'], x="Year", y="Coverage", 
color="Service level",
             hover_data=['Service level'], barmode = 'stack')
 
fig.show()``` **bold** *italic* >quote

我试过了:

```
fig = px.bar(jmp_df, x="Residence Type", y="Coverage", color="Service level",
                hover_data=['Service level'], barmode = 'stack')
     
fig.show()``` **bold** *italic* >quote

但是,这不是实际的组功能。因为我的x轴失去了“年数”,而“覆盖率”则逐年增加

[enter image description here][1]
[enter image description here][2]
[enter image description here][3]

Tags: 数据image类型dfhereservicefigdescription