简体   繁体   English

Python 错误:索引 8 超出尺寸为 8 的轴 0 的范围

[英]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:要处理col1col2等列名,您需要使用 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.

相关问题 python 中的错误:索引 0 超出尺寸为 0 的轴 0 的范围 - error in python : index 0 is out of bounds for axis 0 with size 0 索引错误:索引3超出了尺寸为3的轴1的范围 - Index Error: index 3 is out of bounds for axis 1 with size 3 索引错误:索引 2 超出轴 0 的范围,大小为 2 - Index Error : Index 2 is out of bounds for axis 0 with size 2 Python - 切片错误:IndexError:索引 3 超出了轴 2 大小为 3 的范围 - Python - Slicing error: IndexError: index 3 is out of bounds for axis 2 with size 3 Python 3错误:“ IndexError:索引140超出了轴1的大小100的范围” - Python 3 error: “IndexError: index 140 is out of bounds for axis 1 with size 100” Python 中的“索引 0 超出轴 0 尺寸 0”错误的保护 - Protection against “index 0 is out of bounds for axis 0 with size 0” error in Python python append error index 1超出了大小为1的轴0的范围 - python append error index 1 is out of bounds for axis 0 with size 1 python“IndexError:索引8超出了轴0大小为8的范围” - python "IndexError: index 8 is out of bounds for axis 0 with size 8" python IndexError:索引3超出轴3的大小3 - python IndexError: index 3 is out of bounds for axis 0 with size 3 “python中的索引1超出了0号轴的范围” - “index 1 is out of bounds for axis 0 with size 1” in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM