将堆叠的 table 绘制成条形图 - Pandas

Plotting a stacked table into a barchart - Pandas

我使用

将我的 table 汇总为以下 table
test_df = countData_df
stackedTable =  test_df.reset_index().pivot_table(values='volume', index=['address', 'direction'], 
aggfunc='sum')

noe 我想创建一个条形图,其中有四种颜色用于条形上的不同方向。每个bar只能有两个选项: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'])

但显示如下

df = df[['direction', 'volume']]
df = df.groupby('direction').sum()

您几乎已经掌握了代码。要修复情节,您需要拆开方向。在示例中,方向是索引的一部分,但它应该是某种体积的分组元素。

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'])