尝试使用 df.plot(kind='box) 创建多个箱线图并收到“IndexError: index 0 is out of bounds for axis 0 with size 0”

Posted

技术标签:

【中文标题】尝试使用 df.plot(kind=\'box) 创建多个箱线图并收到“IndexError: index 0 is out of bounds for axis 0 with size 0”【英文标题】:Trying to create multiple boxplots using df.plot(kind='box) and receiving "IndexError: index 0 is out of bounds for axis 0 with size 0"尝试使用 df.plot(kind='box) 创建多个箱线图并收到“IndexError: index 0 is out of bounds for axis 0 with size 0” 【发布时间】:2021-10-26 21:02:39 【问题描述】:

我正在完成 Jason Brownlee 的“Machine Learning Mastery with Python”中的练习,在第 21 章中,我们使用Sonar dataset found in the UCI repository。

我已阅读 sonar.all-data 文件:

file = 'datasets/sonar.all-data'
df = pd.read_csv(file, sep=',', header=None)

数据的形状为(208, 61)

我现在想创建一个箱线图网格,将 df 中的每一列作为它自己的图。

df.plot(kind='box', subplots=True, layout=(8,8),
        sharex=False, sharey=False, figsize=(12, 12))

在笔记本中执行此代码时,出现以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/core/series.py in __setitem__(self, key, value)
   1061         try:
-> 1062             self._set_with_engine(key, value)
   1063         except (KeyError, ValueError):

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/core/series.py in _set_with_engine(self, key, value)
   1094         # fails with AttributeError for IntervalIndex
-> 1095         loc = self.index._engine.get_loc(key)
   1096         # error: Argument 1 to "validate_numeric_casting" has incompatible type

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

During handling of the above exception, another exception occurred:

IndexError                                Traceback (most recent call last)
/var/folders/v1/yp29bgzj6631r45byvnx3tc00000gn/T/ipykernel_29179/2130552721.py in <module>
----> 1 df.plot(kind='box', subplots=True, layout=(8,8), sharex=False, sharey=False, figsize=(12,12));

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
    970                     data.columns = label_name
    971 
--> 972         return plot_backend.plot(data, kind=kind, **kwargs)
    973 
    974     __call__.__doc__ = __doc__

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/plotting/_matplotlib/__init__.py in plot(data, kind, **kwargs)
     69             kwargs["ax"] = getattr(ax, "left_ax", ax)
     70     plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71     plot_obj.generate()
     72     plot_obj.draw()
     73     return plot_obj.result

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py in generate(self)
    286         self._compute_plot_data()
    287         self._setup_subplots()
--> 288         self._make_plot()
    289         self._add_table()
    290         self._make_legend()

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/plotting/_matplotlib/boxplot.py in _make_plot(self)
    144                 )
    145                 self.maybe_color_bp(bp)
--> 146                 self._return_obj[label] = ret
    147 
    148                 label = [pprint_thing(label)]

~/Desktop/Python/Machine Learning Mastery with Python/mlmp_venv/lib/python3.9/site-packages/pandas/core/series.py in __setitem__(self, key, value)
   1065             if is_integer(key) and self.index.inferred_type != "integer":
   1066                 # positional setter
-> 1067                 values[key] = value
   1068             else:
   1069                 # GH#12862 adding a new key to the Series

IndexError: index 0 is out of bounds for axis 0 with size 0

有人可以帮我理解这个错误的含义、导致错误的原因以及如何解决它吗?

提前致谢!

【问题讨论】:

此错误表示索引key 为零,并且变量values 的大小也为0。values 的形状是什么? 【参考方案1】:

如果列标题未命名,则此箱线图不起作用。为每一列命名后,箱形图应该可以工作了。

【讨论】:

以上是关于尝试使用 df.plot(kind='box) 创建多个箱线图并收到“IndexError: index 0 is out of bounds for axis 0 with size 0”的主要内容,如果未能解决你的问题,请参考以下文章

将分组刻度添加到条形图

熊猫散点图日期时间

如何在 Python 中排列 x 轴标签

Matplotlib1

对pandas的dataframe绘图并保存

使用 matplotlib 条形图设置列顺序