IndexError:数组索引过多

时间:2019-02-22 20:47:37

标签: python pandas visualization numpy

我是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:数组索引过多

我对这里的探针有些困惑,因为我为另一个可以使用的笔记本设置了相同的设置。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

matplotlib.pyplot.subplots创建一个图形和坐标轴数组。 axes数组的大小取决于您要创建的子图的数量。自从经过(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]

希望这会有所帮助。

相关问题