【发布时间】:2017-09-01 04:38:23
【问题描述】:
我基本上有以下脚本。但它在运行过程中失败,并在axarr[0].plot(x,y) 行出现以下错误TypeError: 'Figure' object does not support indexing
。我试图四处搜索,但在创建子图时发现了类似的错误......而且我只添加/替换数据(我不确定,因为它是一个 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()
我可能会收到此错误,因为我在循环中使用了一个图形,但它看起来在迭代 1 中失败了。那么我做错了什么或者有什么可以更好地使它起作用(几乎等于 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部分。
标签: python-3.x matplotlib indexing