简体   繁体   English

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

[英]How to make stacked bar plot for Pandas DataFrame work?

I have a DataFrame, consisting of several timeseries like this: 我有一个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

Plotting a simple line diagram works like a charm, even with automatic legend: 绘制简单的线图就像魅力一样,即使使用自动图例:

df.plot()

In the documentation, if find the optional parameter stacked (boolean, default False) with the description "If True, create stacked bar plot. Only valid for DataFrame input" . 在文档中,如果找到可选参数stacked (boolean,默认为False),其描述为“如果为True,则创建堆积条形图。仅对DataFrame输入有效” So I type 所以我输入

df.plot(stacked=True)

and receive the following TypeError message: 并收到以下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"

Did I call the function in a wrong way or is this a bug? 我是以错误的方式调用该函数还是这个错误?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM