[英]IndexError: too many indices for array
I am new to SE DS so please let me know if I need to edit my question. 我是SE DS的新手,所以如果需要编辑我的问题,请告诉我。
data = pd.read_csv('Desktop/dataset.csv')
# Feature 1
feature_1 = data['expenses']
I have a series representing a feature column from my dataset: 我有一个系列代表我的数据集中的功能列:
feature_1.head()
0 6384.911133
1 5099.380859
2 5501.954590
3 7101.831055
4 5235.987793
Name: expenses, Length: 420, dtype: float64
When I call feature_1.shape it returns (420, ) 当我调用feature_1.shape时,它返回(420,)
I have a figure and axes area set up and plot: 我有一个图和轴区域设置并绘制:
# 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)
It then returns the error IndexError: too many indices for array 然后返回错误IndexError:数组的索引过多
I am a little confused of what the probelm might be here becuase I have the same set up for another notebook that works. 我有点困惑,因为我为另一个可以使用的笔记本设置了相同的设置。 Any thoughts? 有什么想法吗?
matplotlib.pyplot.subplots
creates a figure and axes array. matplotlib.pyplot.subplots
创建一个图形和轴数组。 The size of the axes array is dependent on the number of subplots you are creating. axes数组的大小取决于您要创建的子图的数量。 Since you went by (1, 3)
, you will be having three plots in a single row. 自从经过(1, 3)
以来,您将在一行中包含三个图。 So your axes array shape
attribute describes this. 因此,您的axiss数组shape
属性对此进行了描述。 For example 例如
>>> 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]
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.