大熊猫,分层标记条形图

时间:2015-12-04 20:47:29

标签: python pandas plot charts

询问确切的问题为Is it possible to hierarchically label a matplotlib (pyplot) bar plot?,但对于熊猫来说,因为答案不存在。

This就我自己而言。您可以在In [10]看到,我试图从上面复制解决方案,但它对Pandas不起作用。事实上,我从'" A"中看不到我的标签。和" B"一点都不

我想要两个级别的标签," A"作为第一级和" B"作为次要的。我怎样才能做到这一点?谢谢。

1 个答案:

答案 0 :(得分:1)

首先需要设置索引。

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar']*2,
                   'B' : ['one', 'one', 'two', 'three',
                         'two', 'two', 'one', 'three'],
                  'C' : np.random.randn(8),
                  'D' : np.random.randn(8)})


>>> df.set_index(['A', 'B']).plot(rot=90)

enter image description here