gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:46:07 26 4
gpt4 key购买 nike

我有一个 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( bool 值,默认为 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"

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

最佳答案

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

关于python - 如何为 Pandas DataFrame 制作堆积条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17834652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com