【问题标题】:IndexError: too many indices for arrayIndexError:数组的索引过多
【发布时间】:2019-07-17 15:50:20
【问题描述】:

我是 SE DS 的新手,所以如果我需要编辑我的问题,请告诉我。

data = pd.read_csv('Desktop/dataset.csv')

# Feature 1
feature_1 = data['expenses']

我有一个代表数据集中特征列的系列:

feature_1.head()

0      6384.911133
1      5099.380859
2      5501.954590
3      7101.831055
4      5235.987793

Name: expenses, Length: 420, dtype: float64

当我调用 feature_1.shape 时,它​​返回 (420, )

我有一个图形和轴区域设置和绘图:

# Create a figure area with three plots
fig, axes = plt.subplots(1, 3, figsize=(15,4.5))

axes[0, 0].hist(feature_1, bins=5)

然后返回错误IndexError: too many indices for array

我对这里的问题可能有些困惑,因为我为另一个可以工作的笔记本设置了相同的设置。有什么想法吗?

【问题讨论】:

  • 您的笔记本和这段代码是否设置在不同的虚拟环境中?软件包版本可能是一个原因。
  • 你能展示你代码的更多扩展部分吗(尤其是在你调用 plot 函数之前)?
  • 我已经更新了我的代码,现在更好了吗?

标签: python pandas visualization numpy


【解决方案1】:

matplotlib.pyplot.subplots 创建一个图形和坐标区数组。坐标区数组的大小取决于您创建的子图的数量。由于您经过(1, 3),因此您将在一行中拥有三个地块。所以你的轴数组shape 属性描述了这一点。例如

>>> from matplotlib import pyplot as plt
>>> import numpy as np
>>> a = np.random.rand(420)
>>> a.shape
(420,)
>>> fig, axes = plt.subplots(1, 3, figsize=(15,4.5))
>>> axes.shape
(1, 3)
>>> fig, axes = plt.subplots(2, 2, figsize=(15,4.5))
>>> axes.shape
(2, 2)
>>> axes[0].hist(feature_1, bins=5) # gives your histogram in the first subplot. If it was 2x2 subplot, the access would have been axes[i, j]

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2020-09-14
    • 2018-05-23
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多