【问题标题】:Trend curve through data jumps back and forth when it should be smooth通过数据的趋势曲线在应该平滑时来回跳跃
【发布时间】:2016-09-08 11:13:31
【问题描述】:

我想绘制我的数据点的趋势曲线。 我用指数模型的代码做到了这一点:

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()

但是我得到了这个结果:

]

问题

如何通过这些数据获得平滑的趋势曲线?

【问题讨论】:

    标签: python curve points trend


    【解决方案1】:

    一种方法,您可以在绘图前输入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 中出现的顺序连接点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 2013-07-09
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多