Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:列表索引必须是整数,而不是元组,然后将数据合并到一个绘图中_Python_Arrays_Plot - Fatal编程技术网

Python TypeError:列表索引必须是整数,而不是元组,然后将数据合并到一个绘图中

Python TypeError:列表索引必须是整数,而不是元组,然后将数据合并到一个绘图中,python,arrays,plot,Python,Arrays,Plot,我的主代码有一个“TypeError”,所以我编写了一个小代码来尝试重现这个问题。我能够重现错误,但我不确定是什么错了。我试着将数组转换成整数,但没有成功。下面是我一直在使用的小代码,用于尝试纠正错误 x = [5,10,15,20,25] y = [9,10,20,15,25] x0 = np.linspace(0,3) fig, axes = plt.subplots(ncols=3, nrows=4, sharex=True, sharey=True, figsize=(7,7)) f

我的主代码有一个“TypeError”,所以我编写了一个小代码来尝试重现这个问题。我能够重现错误,但我不确定是什么错了。我试着将数组转换成整数,但没有成功。下面是我一直在使用的小代码,用于尝试纠正错误

x = [5,10,15,20,25]
y = [9,10,20,15,25]
x0 = np.linspace(0,3)

fig, axes = plt.subplots(ncols=3, nrows=4, sharex=True, sharey=True, figsize=(7,7))

for i, ax in enumerate(axes.flatten()):
    ax.scatter(x[:,i], y[:,i], s=10)
    m, b = np.polyfit(x[:,i], y[:,i], 1)
    ax.plot(x0, m*x0+b, lw=1., label="y={:.2f}x+{:.2f}".format(m,b))
    ax.legend()

plt.tight_layout()    
plt.show()
如何将数组的索引转换为整数,使它们不是元组?我希望所有相同的数据保持不变。如果需要,您可以扩展阵列并使其更长,以便向我确切地说明问题所在

在这之后,我想将所有的曲线图数据合并到一个曲线图上,这样我就可以看到所有的线性回归线和点都绘制在同一个图表上。

试试下面的代码

x = [5,10,15,20,25]
y = [9,10,20,15,25]
x0 = np.linspace(0,3)

fig, axes = plt.subplots(ncols=3, nrows=4, sharex=True, sharey=True, figsize=(7,7))

for i, ax in enumerate(axes.flatten()):
    ax.scatter(np.array(x[:i]), np.array(y[:i]), 10)
    m, b = np.polyfit(np.array(x[:i+1]), np.array(y[:i+1]), 1)
    ax.plot(x0, m*x0+b, lw=1., label="y={:.2f}x+{:.2f}".format(m,b))
    ax.legend()

plt.tight_layout()    
plt.show()

例如,
x[:,i]
您正在索引它,因为它是一个numpy数组,但它只是一个常规列表。我应该索引一个常规列表,还是应该将代码更改为其他内容。我不知道您到底想实现什么。如果您只想根据
y
绘制
x
,我认为您根本不需要索引它们。基本上,我希望代码生成多个绘图,而无需复制和粘贴代码,然后在最后,我希望将所有绘图中的所有数据和线性回归线合并到一个绘图中。我的主代码有两个很长的列表;我在这里用了短的。事实上,我可能已经解决了这个问题。谢谢当我复制并粘贴你的代码到我的代码时,我仍然会遇到同样的错误。我刚刚检查了一下,这似乎有效!我现在只需要在我的主代码中尝试一下。