如何为Pandas DataFrame制作堆积条形图?

时间:2013-07-24 12:54:41

标签: python pandas

我有一个DataFrame,包含几个这样的时间序列:

import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30, 40], 
                   'B': [15, 17, 16, 12],
                   'C': [33, 11, 49, 20]})
df
    A   B   C
0  10  15  33
1  20  17  11
2  30  16  49
3  40  12  20

绘制简单的线图就像魅力一样,即使是自动图例:

df.plot()

在文档中,如果找到可选参数stacked(布尔值,默认为False),其描述为“如果为True,则创建堆积条形图。仅对DataFrame输入有效”。所以我输入

df.plot(stacked=True)

并收到以下TypeError消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-110-8a5cc63390e6> in <module>()
----> 1 df.plot(stacked=True)

C:\Python27\lib\site-packages\pandas\tools\plotting.py in plot_frame(frame, x, y, subplots, sharex, sharey, use_index, f
igsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secon
dary_y, **kwds)
   1564                      logy=logy, sort_columns=sort_columns,
   1565                      secondary_y=secondary_y, **kwds)
-> 1566     plot_obj.generate()
   1567     plot_obj.draw()
   1568     if subplots:

C:\Python27\lib\site-packages\pandas\tools\plotting.py in generate(self)
    798         self._compute_plot_data()
    799         self._setup_subplots()
--> 800         self._make_plot()
    801         self._post_plot_logic()
    802         self._adorn_subplots()

C:\Python27\lib\site-packages\pandas\tools\plotting.py in _make_plot(self)
   1176
   1177                 try:
-> 1178                     newline = plotf(*args, **kwds)[0]
   1179                     lines.append(newline)
   1180                     leg_label = label

C:\Python27\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, **kwargs)
   3994         lines = []
   3995
-> 3996         for line in self._get_lines(*args, **kwargs):
   3997             self.add_line(line)
   3998             lines.append(line)

C:\Python27\lib\site-packages\matplotlib\axes.pyc in _grab_next_args(self, *args, **kwargs)
    328                 return
    329             if len(remaining) <= 3:
--> 330                 for seg in self._plot_args(remaining, kwargs):
    331                     yield seg
    332                 return

C:\Python27\lib\site-packages\matplotlib\axes.pyc in _plot_args(self, tup, kwargs)
    316         ncx, ncy = x.shape[1], y.shape[1]
    317         for j in xrange(max(ncx, ncy)):
--> 318             seg = func(x[:,j%ncx], y[:,j%ncy], kw, kwargs)
    319             ret.append(seg)
    320         return ret

C:\Python27\lib\site-packages\matplotlib\axes.pyc in _makeline(self, x, y, kw, kwargs)
    266                      **kw
    267                      )
--> 268         self.set_lineprops(seg, **kwargs)
    269         return seg
    270

C:\Python27\lib\site-packages\matplotlib\axes.pyc in set_lineprops(self, line, **kwargs)
    207             funcName = "set_%s"%key
    208             if not hasattr(line,funcName):
--> 209                 raise TypeError('There is no line property "%s"'%key)
    210             func = getattr(line,funcName)
    211             func(val)

TypeError: There is no line property "stacked"

我是以错误的方式调用该函数还是这是一个错误?

1 个答案:

答案 0 :(得分:2)

df.plot(kind='barh', stacked=True)