簡體   English   中英

當數據應該平滑時,數據的趨勢曲線會來回跳轉

[英]Trend curve through data jumps back and forth when it should be smooth

我想繪制數據點的趨勢曲線。 我用這個代碼用指數模型做到了這一點:

with open(file,'r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
next(plots)
x=[]
y=[]
for row in plots:
    x.append(float(row[1]))
    y.append(float(row[3]))
plt.plot(x, y, 'ro',label="Original Data")
x = np.array(x, dtype=float) #transform your data in a numpy array of floats 
y = np.array(y, dtype=float) #so the curve_fit can work



def func(x, a, b, c):
    return (a*np.exp(-b*x)+c)

popt, pcov = curve_fit(func, x, y)
ypredict=func(x, *popt)

plt.plot(x, ypredict, '--', label="Fitted Curve")  
plt.legend(loc='upper left')
plt.show()

但我得到了這個結果:

在此輸入圖像描述 ]

如何通過這些數據獲得平滑的趨勢曲線?

一種方法,你可以在繪圖前放入x.sort()

x.sort()
ypredict=func(x, *popt)

另一種方法是使用這樣的東西(在我看來更好的情節),

# 1000 evenly spaced points over the range of x values
x_plot = np.linspace(x.min(), x.max(), 1000)
y_plot=func(x_plot, *popt)    

然后使用x_ploty_plot作為趨勢線,它應該看起來很好。 問題很可能是你的x值不是單調的,因此你的線圖是按照它們在x出現的順序連接點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM