图形的平滑在范围内产生了巨大的差异 [英] Smoothing of graph gives a huge difference in the range

查看:41
本文介绍了图形的平滑在范围内产生了巨大的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用上面的 x,y 坐标绘制平滑曲线.但是,我得到的图形超出了我的数据范围.我的代码片段在这里.

I am trying to plot a smooth curve using the x,y cordinates above. Howsoever the graph which i get is out of the range of my data. The snippet of my code is here.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline

ylist = [0.36758563074352546, 0.27634194831013914, 0.22261484098939929, 0.10891089108910891, 0.31578947368421051, 0.086956521739130432, 0.27272727272727271, 0.18181818181818182, 0.0, 0.0, 0.92000000000000004, 0.0, 0.10526315789473684, 0.23333333333333334]
xlist = [0.025000000000000001, 0.075000000000000011, 0.125, 0.17500000000000002, 0.22500000000000001, 0.27500000000000002, 0.32500000000000001, 0.375, 0.42500000000000004, 0.47500000000000003, 0.52500000000000002, 0.57500000000000007, 0.625, 0.97500000000000009]

xlist_smooth = np.linspace(xlist.min(), xlist.max(), 100)
ylist_smooth = spline(xlist, ylist, xlist_smooth)
plt.plot(xlist_smooth,ylist_smooth)

我得到以下曲线作为输出

I get the following curve as the output

推荐答案

我认为这里的问题是高阶样条插值不适合平滑数据.

I think the problem here is that spline interpolation of a higher order is not suitable for smoothing your data.

下面我绘制了 0 到 3 阶的样条插值.您看到的是,一旦您要求导数(2 阶及更高阶)的连续性,您就会遇到最后两点的问题.

Below I plotted spline interpolations of order 0 to 3. What you see is that once you demand continuity of the derivative (order 2 and higher) you run into problems with the last two points.

我想在这里选择样条插值不是一个好的选择.插值假设没有测量误差,并且您的数据中似乎有明显的异常值.

I guess choosing a spline interpolation is not a good choice here. Interpolation assumes that there are no measurement errors and you seem to have a clear outlier in your data.

根据您要在此处执行的操作,拟合分段连续样条曲线(order = 1)可能适合您.否则,您可能必须寻找其他的平滑策略.

Depending on what you want to do here, fitting a piecewise continuous spline (order=1) may be fine for you. Otherwise you probably have to look for a different smoothing strategy.

import numpy as np
from scipy.interpolate import spline
import matplotlib.pyplot as plt

ylist = [0.36758563074352546, 0.27634194831013914, 0.22261484098939929,
         0.10891089108910891, 0.31578947368421051, 0.086956521739130432, 
         0.27272727272727271, 0.18181818181818182, 0.0, 0.0,
         0.92000000000000004, 0.0, 0.10526315789473684, 0.23333333333333334]
xlist = [0.025000000000000001, 0.075000000000000011, 0.125, 0.17500000000000002,
         0.22500000000000001, 0.27500000000000002, 0.32500000000000001, 0.375,
         0.42500000000000004, 0.47500000000000003, 0.52500000000000002, 
         0.57500000000000007, 0.625, 0.97500000000000009]

xlist_smooth = np.linspace(min(xlist), max(xlist), 100)

fig, axes = plt.subplots(4,1, sharex=True)
for order, ax in enumerate(axes):
    ylist_smooth = spline(xlist, ylist, xlist_smooth, order=order)
    ax.plot(xlist_smooth, ylist_smooth,  label="spline order %s" % order)
    ax.scatter(xlist, ylist, label="knots")
    ax.set_ylim(min(xlist)-1,max(xlist)+1)
    ax.legend()

这篇关于图形的平滑在范围内产生了巨大的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆