我基本上有以下的脚本。但在运行过程中出现以下错误:TypeError: 'Figure'对象不支持索引
在第axarr [0] .plot(x,y)
行。我尝试搜索,但发现与创建子图时类似的错误...而我只是添加/替换数据(我不确定,因为它是Matlab文件的副本,而我没有Matlab)。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 2, 0.01)
for idx in range(1, 10):
a = 1 + (idx - 1) / 10
y = a ** x
axarr, fig = plt.subplots(1,1)
axarr[0].plot(x,y)
axarr.axis([0, 4, 0, 85])
axarr[0].grid(True)
plt.show()
可能我之所以出现这个错误是因为我在循环中使用了一个 figure,但似乎它在第一次迭代就失败了。那么我做错了什么,或者有什么更好的方法让它工作(与 matlab 文件几乎相等,请参见下面脚本的部分)?
希望有人能帮忙。
与 matlab 文件相同的样本文件如下:
x = 0:0.01:4;
for idx = 1:10
a = 1 + (idx-1)/10;
y = a.^x;
z = 2 * y
subplot(111)
plot(x,y)
hold on
plot(x(1:400),z)
axis([0 4 0 85])
pause
hold off
end
fix,axarr = plt.subplots(1,1)
(可以省略1,1
部分)。 - Thomas Kühn