【问题标题】:Plotting a stacked table into a barchart - Pandas将堆积的表格绘制成条形图 - Pandas
【发布时间】:2021-05-09 03:55:57
【问题描述】:

我使用

将我的表格汇总到下表中
test_df = countData_df
stackedTable =  test_df.reset_index().pivot_table(values='volume', index=['address', 'direction'], 
aggfunc='sum')

我想创建一个条形图,条形图上有四种颜色用于不同的方向。每个条形只能有两个选项:N 或 S; E或W

所以我完成了这条线

test_df.reset_index().pivot_table(values='volume', index=['address', 'direction'], 
aggfunc='sum').plot(kind = 'bar', stacked = True,  color = ['b','g','r','m'])

但显示如下

【问题讨论】:

    标签: python pandas stacked-chart


    【解决方案1】:

    你几乎已经有了代码。要修复情节,您需要取消堆叠方向。在示例中,方向是索引的一部分,但它应该是某种用于交易量的分组元素。

    test_df.reset_index().pivot_table(values='volume', index=['address', 'direction'], 
    aggfunc='sum').unstack("direction").plot(kind = 'bar', stacked = True,  color = ['b','g','r','m'])
    

    【讨论】:

      【解决方案2】:
      df = df[['direction', 'volume']]
      df = df.groupby('direction').sum()
      

      【讨论】:

        猜你喜欢
        • 2016-05-24
        • 2018-11-26
        • 1970-01-01
        • 2012-09-17
        • 2021-12-27
        • 2018-09-28
        相关资源
        最近更新 更多