[英]Python error: index 8 is out of bounds for axis 0 with size 8
I'm tryng to run this code to generate some graphics but everytime this error appears.我正在尝试运行这段代码来生成一些图形,但每次都会出现此错误。 parte of my code:
我的代码的一部分:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(8, sharex=False, sharey=False,figsize=(15,15))
fig.suptitle('Todas Features para estado Normal ')
for i in data_n.columns:
axs[i].plot(data_n[i])
axs[i].set_title('Coluna {} do Dataset'.format(i))
error:错误:
IndexError Traceback (most recent call last)
<ipython-input-129-0fef11f34a8c> in <module>
3 fig.suptitle('Todas Features para estado Normal ')
4 for i in data_n.columns:
----> 5 axs[i].plot(data_n[i])
6 axs[i].set_title('Coluna {} do Dataset'.format(i))
IndexError: index 8 is out of bounds for axis 0 with size 8
when i try to change de number os subplots, nothing changes.当我尝试更改 de number os 子图时,没有任何变化。
It's not clear what your data_n
variable is but I'm guessing it's probably a pandas.DataFrame
object.目前尚不清楚您的
data_n
变量是什么,但我猜它可能是pandas.DataFrame
object。
Python for
loops are 0-indexed and your IndexError
error is occuring at index 8, so data_n.columns
is probably an iterable pandas.DataFrame.columns
object that has more than 8 values. Python
for
循环是 0 索引的,您的IndexError
错误发生在索引 8 处,因此data_n.columns
可能是可迭代的pandas.DataFrame.columns
object 具有超过 8 个值。 You can check this with len(data_n.columns)
.您可以使用
len(data_n.columns)
进行检查。
Consider trying:考虑尝试:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(len(data_n.columns), sharex=False, sharey=False,figsize=(15,15))
fig.suptitle('Todas Features para estado Normal ')
for i in data_n.columns:
axs[i].plot(data_n[i])
axs[i].set_title('Coluna {} do Dataset'.format(i))
This will only work if the names of the data_n columns are integers, which they may or may not be.这仅在 data_n 列的名称是整数时才有效,它们可能是也可能不是。 To handle column names such as
col1
, col2
, etc. you'll need to use an integer index, as in this basic example:要处理
col1
、 col2
等列名,您需要使用 integer 索引,如以下基本示例所示:
indexer = 0
for i in data_n.columns:
axs[indexer].plot(data_n[i])
axs[indexer].set_title('Coluna {} do Dataset'.format(i))
indexer += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.