pyplot等值线图的指数误差

时间:2017-04-21 19:58:52

标签: python matplotlib indexing scipy curve-fitting

我正在尝试使用来自电路的实验数据使用matplotlib.pyplot创建等高线图。我正在使用带有3个参数的scipy curve_fit函数(由大小为251的数组给出),但我不断收到索引错误。这是我的情节代码的一部分:

Npts = 10000
v1scan = np.zeros(Npts) #voltage1
v2scan =np.zeros(Npts) #voltage2
tauscan = np.zeros(Npts) #time constant
chi_dof = np.zeros(Npts)
udata = np.zeros(Npts) #uncertainty
i = 0

dof = len(time) - len(popt)

for v1par in np.linspace(0, 8, 100, True):
    for v2par in np.linspace(0, 8, 100, True): 
        for taupar in np.linspace(5*(10**-5), 7*(10**-5), 100, True):
            v1scan[i] = v1par
            v2scan[i] = v2par
            tauscan[i] = taupar
            dymin = (vcap - capvoltage(time, v1par, v2par, taupar))/(0.02*vcap) #vectorized
            chi_dof[i] = sum(dymin*dymin)/dof
            i = i + 1
plt.figure() #creates new figure
ncols = 10
plt.tricontourf(v1par, v2par, taupar, chi_dof, ncols)
plt.colorbar()
plt.show()

我得到的错误信息是:

IndexError                                Traceback (most recent call last)
<ipython-input-175-7562791ba165> in <module>()
     12     for v2par in np.linspace(0, 8, 100, True):
     13         for taupar in np.linspace(5*(10**-5), 7*(10**-5), 100, True):
---> 14             v1scan[i] = v1par
     15             v2scan[i] = v2par
     16             tauscan[i] = taupar

IndexError: index 10000 is out of bounds for axis 0 with size 10000

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

100次100次100 = 1000000而不是10000

一旦达到10000(其最大值应为9999),您就会尝试访问内存不足区域。

编辑:修复:

Npts = 1000000