【问题标题】:Index Error for pyplot contour plotpyplot 等高线图的索引错误
【发布时间】:2017-09-18 22:46:23
【问题描述】:

我正在尝试使用来自电路的实验数据创建带有 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

我该如何解决这个问题?

【问题讨论】:

    标签: python matplotlib indexing scipy curve-fitting


    【解决方案1】:

    100 乘以 100 乘以 100 = 1000000 而不是 10000

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

    编辑:修复:

    Npts = 1000000
    

    【讨论】:

    • 这确实使错误消息消失了(谢谢)。虽然现在情节根本不会显示(没有错误消息,只是什么都没有发生)。
    • 嗯,这是一个不同的问题。一个简单的情节有效吗? import matplotlib.pyplot as pltplt.plot([1, 2, 3])plt.show()(全部在一个文件中)
    猜你喜欢
    • 2015-01-28
    • 2016-02-20
    • 1970-01-01
    • 2017-12-08
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    相关资源
    最近更新 更多