使用Groupby子集创建多行图

时间:2018-02-28 21:58:31

标签: python-3.x pandas matplotlib pandas-groupby

我正在使用Pandas和Matplotlib进行可视化。我在创建图表时遇到了麻烦,即使我觉得我拥有了所有需要的信息。

from matplotlib import rcParams
%matplotlib inline
rcParams['figure.figsize'] = 5, 4
sb.set_style('ticks')
B = df.groupby(['DATE']).BOROUGH.value_counts()
B

DATE        BOROUGH      
2018-02-14  QUEENS           205
            BROOKLYN         160
            MANHATTAN        123
            BRONX             85
            STATEN ISLAND     30
2018-02-15  QUEENS           177
            BROOKLYN         160
            MANHATTAN        130
            BRONX             84
            STATEN ISLAND     24
2018-02-16  QUEENS           152
            BROOKLYN         125
            MANHATTAN        118
            BRONX             90
            STATEN ISLAND     24
2018-02-17  QUEENS           163
            MANHATTAN        138
            BROOKLYN         130
            BRONX             76
            STATEN ISLAND     16
2018-02-18  QUEENS           138
            BROOKLYN         104
            MANHATTAN         96
            BRONX             54
            STATEN ISLAND     22
2018-02-19  QUEENS           132
            BROOKLYN         127
            MANHATTAN         60
            BRONX             53
            STATEN ISLAND     11
2018-02-20  QUEENS           141
            BROOKLYN         134
            MANHATTAN         73
            BRONX             57
            STATEN ISLAND      8

如何绘制这个子集,其中x是日期,每个Borough在同一图表上形成一条单独的行?

1 个答案:

答案 0 :(得分:0)

你可以取消堆叠你的系列并将其绘制为常规DF:

B.unstack().plot()

enter image description here

“拆散”的结果:

In [41]: B.unstack()
Out[41]:
BOROUGH     BRONX  BROOKLYN  MANHATTAN  QUEENS  STATEN ISLAND
DATE
2018-02-14     85       160        123     205             30
2018-02-15     84       160        130     177             24
2018-02-16     90       125        118     152             24
2018-02-17     76       130        138     163             16
2018-02-18     54       104         96     138             22
2018-02-19     53       127         60     132             11
2018-02-20     57       134         73     141              8