Python Pandas绘制条-使用列值调整宽度

时间:2018-08-28 14:14:51

标签: python pandas

我正在使用python / pandas数据框绘制条形图,但是使用数组或df列来调整条形宽度没有成功,像这样:

df.plot.bar(ax=axes[i], width=df.width, stacked=True, color=colors[i], logy=logy)

其中df.width是我数据框中的一列。堆栈跟踪:

File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 3090, in bar
    return self(kind='bar', x=x, y=y, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 2941, in __call__
    sort_columns=sort_columns, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1977, in plot_frame
    **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1804, in _plot
    plot_obj.generate()
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 267, in generate
    self._post_plot_logic(ax, self.data)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1276, in _post_plot_logic
    e_edge = self.ax_pos[-1] + 0.25 + self.bar_width + self.lim_offset
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/series.py", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/numeric.py", line 358, in get_value
    loc = self.get_loc(k)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/numeric.py", line 419, in get_loc
    tolerance=tolerance)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 379, in pandas._libs.hashtable.Float64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: -1.0

我尝试使用列表,而不是使用df列,但是我遇到了问题:

wl = df.width.tolist()
df.plot.bar(ax=axes[i], width=wl, stacked=True, color=colors[i], logy=logy)

Stacktrace:

  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 3090, in bar
    return self(kind='bar', x=x, y=y, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 2941, in __call__
    sort_columns=sort_columns, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1977, in plot_frame
    **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1802, in _plot
    plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/plotting/_core.py", line 1185, in __init__
    self.tickoffset = self.bar_width * pos
TypeError: can't multiply sequence by non-int of type 'float'

感谢您的帮助。 下面是重现该问题的最小代码:

import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=1, sharex=True)

my_dict = {'width': {0.0: 3.0, 3.0: 2.0, 5.0: 1.0}, 'kbps1': {0.0: 10.0, 3.0: 20.0, 5.0: 30}, 'kbps2': {0.0: 5.0, 3.0: 10.0, 5.0: 15.0}, 'kbps3': {0.0: 3.0, 3.0: 10.0, 5.0: 10.0}}

ndf = pd.DataFrame(my_dict)
wl = ndf.width.tolist()
ndf = ndf[['kbps1','kbps2','kbps3']]
# Problem
ndf.plot.bar(ax=ax, width=wl, stacked=True)
# Working with scalar
ndf.plot.bar(ax=ax, width=1.0, stacked=True)

1 个答案:

答案 0 :(得分:0)

我使用plt.bar而不是df.plot.bar解决了该问题。 解决方法如下:

fig, ax = plt.subplots(nrows=1, ncols=1)
my_dict = {'width': {0.0: 3.0, 3.0: 2.0, 5.0: 1.0}, 'kbps1': {0.0: 10.0, 3.0: 20.0, 5.0: 30}, 'kbps2': {0.0: 5.0, 3.0: 10.0, 5.0: 15.0}, 'kbps3': {0.0: 3.0, 3.0: 10.0, 5.0: 10.0}}
ndf = pd.DataFrame(my_dict)

for c in ndf.drop(['width'], axis=1).columns:
    ax.bar(ndf.index, ndf[c], align='edge', width=ndf.width)

不知道大熊猫“宽度”为何如此明显的行为。