python No plot或namererror UPDATE plot是可见的,但不是应该显示的

2024-09-30 14:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过polyfit和polyval创建一个使用多项式的“滚动样条曲线”。你知道吗

但是,我要么得到一个错误,即“偏移量”没有定义。。。或者,样条曲线不打印。你知道吗

我的代码在下面,请提供建议或见解。我是个新手。你知道吗

import numpy as np
from matplotlib import pyplot as plt

x = np.array([ 3893.50048173,  3893.53295003,  3893.5654186 ,  3893.59788744,
        3893.63035655,  3893.66282593,  3893.69529559,  3893.72776551,
        3893.76023571,  3893.79270617,  3893.82517691,  3893.85764791,
        3893.89011919,  3893.92259074,  3893.95506256,  3893.98753465,
        3894.02000701,  3894.05247964,  3894.08495254])
y = np.array([ 0.3629712 ,  0.35187397,  0.31805825,  0.3142261 ,  0.35417492,
        0.34981215,  0.24416184,  0.17012087,  0.03218199,  0.04373861,
        0.08108644,  0.22834105,  0.34330638,  0.33380814,  0.37836754,
        0.38993407,  0.39196328,  0.42456769,  0.44078106])
e = np.array([ 0.0241567 ,  0.02450775,  0.02385632,  0.02436235,  0.02653321,
        0.03023715,  0.03012712,  0.02640219,  0.02095554,  0.020819  ,
        0.02126918,  0.02244543,  0.02372675,  0.02342232,  0.02419184,
        0.02426635,  0.02431787,  0.02472135,  0.02502038])

xk = np.array([])
yk = np.array([])

w0 = np.where((y<=(e*3))&(y>=(-e*3)))
w1 = np.where((y<=(1+e*3))&(y>=(1-e*3)))
mask = np.ones(x.size)
mask[w0] = 0
mask[w1] = 0

for i in range(0,x.size):
    if mask[i] == 0:
        if ((abs(y[i]) < abs(e[i]*3))and(abs(y[i])<(abs(y[i-1])-abs(e[i])))):
            imin = i-2
            imax = i+3
            if imin < 0:
                imin = 0
            if imax >= x.size:
                imax = x.size
            offset = np.mean(x)
            for order in range(20):
                coeff = np.polyfit(x-offset,y,order)
                model = np.polyval(coeff,x-offset)
                chisq = ((model-y)/e)**2
                chisqred = np.sum(chisq)/(x.size-order-1)
                if chisqred < 1.5:
                    break
            xt = x[i]
            yt = np.polyval(coeff,xt-offset)
    else:
        imin = i-1
        imax = i+2
        if imin < 0:
            imin = 0
        if imax >= x.size:
            imax = x.size
        offset = np.mean(x)
        for order in range(20):
            coeff = np.polyfit(x-offset,y,order)
            model = np.polyval(coeff,x-offset)
            chisq = ((model-y)/e)**2
            chisqred = np.sum(chisq)/(x.size-order-1)
            if chisqred < 1.5:
                break
        xt = x[i]
        yt = np.polyval(coeff,xt-offset)

    xk = np.append(xk,xt)
    yk = np.append(yk,yt)

#print order,chisqred
################################

plt.plot(x,y,'ro')
plt.plot(xk+offset,yk,'b-') # This is the non-plotting plot
plt.show()

################################

更新


所以我编辑了代码,删除了所有不适用于这个小数据样本的if条件。你知道吗

我还添加了我所做的更改,允许代码绘制所需的点。。。然而,现在情节清晰可见,我有一个新问题。你知道吗

这个图不是一个多项式的顺序,代码告诉我它应该是。你知道吗

在plot命令之前,我添加了一个print来显示多项式和chisqred的顺序,以确定它是否正常工作。你知道吗


Tags: 代码sizeifnporderpltabsarray
1条回答
网友
1楼 · 发布于 2024-09-30 14:25:23

首先,感谢您提供一个独立的示例(没有多少新手这么做)!如果您想改进您的问题,您应该从示例中删除所有调试代码,因为现在它会使代码变得混乱。这段代码很长,不太容易解释。(至少对我来说,问题也可能出在我耳中。)


让我们把问题从头到尾展开。得到空图的最接近的原因是有空的xkyk(空数组)。你知道吗

为什么?这是因为你有19个点,因此你的for循环基本上是:

for i in range(12, 19-1-12):
    ...

从12到6没有什么可以迭代的!所以实际上你的循环只运行了零次,没有任何东西附加到xkyk。你知道吗

同样的解释解释了offset的问题。如果循环从未运行过,那么在yout plot命令(xk+offset)中没有定义offset,因此NameError。你知道吗


这是最简单的部分。但是,我不太理解你的代码。尤其是循环order形式为0..19的循环看起来很奇怪,因为只有最后一个循环的结果才会被使用。也许有什么要解决的?你知道吗

(如果您在分析后仍然对代码有问题,请修复您可以解决的问题,尽可能简化代码,并编辑您的问题。然后我们可以再看看这个!)你知道吗

相关问题 更多 >