首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巧妙地:条形堆叠图

巧妙地:条形堆叠图
EN

Stack Overflow用户
提问于 2022-10-28 12:45:51
回答 1查看 37关注 0票数 1

我有一个Dataframe,其数据如下:

代码语言:javascript
复制
data={'A': {'2020_01': 3, '2020_02': 3, '2020_03': 1, '2020_04': 3, '2020_05': 1},
 'B': {'2020_01': 0, '2020_02': 0, '2020_03': 3, '2020_04': 0, '2020_05': 2},
 'other': {'2020_01': 0,
  '2020_02': 0,
  '2020_03': 3,
  '2020_04': 0,
  '2020_05': 2},
 'total': {'2020_01': 3,
  '2020_02': 3,
  '2020_03': 7,
  '2020_04': 3,
  '2020_05': 5}}
df = pd.DataFrame(data)

我想用X来表示A,B,other的日期和y叠加值

只有一个酒吧我能做到:

代码语言:javascript
复制
import plotly.express as px
fig = px.bar(df, x=df.index, y='A', text_auto=True,
             labels={'A':'bananas'}, height=400)
fig.show()

我要怎么做?我查了一堆文件,但没有结果:https://community.plotly.com/t/plotly-express-bar-ignores-barmode-group/31511 https://plotly.com/python/bar-charts/ Plotly px.bar not stacking with barmode='stack' .结果:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-28 13:34:40

所有你应该做的事情如下:

  1. 重置索引并将其用作列.

  1. 使用melt函数以适当的形式转换数据。

  1. 使用barmode="stack"绘制堆叠条形图。

将plotly.express导入为px df.reset_index(inplace=True) df = df.iloc:,:-1.熔体(id_vars=‘index’,var_name=‘类别’,value_name='count')图= px.histogram(df,x=“索引”,y=“计数”,text_auto=True,color=“类别”,barmode=“堆栈”,labels={‘A’:‘香蕉’},height=400) fig.show()

输出:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74235638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档