不均匀间隔点之间的直观插值 [英] Intuitive interpolation between unevenly spaced points

查看:30
本文介绍了不均匀间隔点之间的直观插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图表,我想使用 Python 和 Matplotlib 将其数字化为高质量的出版物级图表:

I have the following graph that I want to digitize to a high-quality publication grade figure using Python and Matplotlib:

我使用数字化仪程序从 3 个数据集之一中抓取了一些样本:

I used a digitizer program to grab a few samples from one of the 3 data sets:

x_data = np.array([
1,
1.2371,
1.6809,
2.89151,
5.13304,
9.23238,
])

y_data = np.array([
0.0688824,
0.0490012,
0.0332843,
0.0235889,
0.0222304,
0.0245952,
])

我已经尝试了 3 种不同的方法来通过这些数据点拟合曲线.第一种方法是使用 scipy.interpolate import spline

I have already tried 3 different methods of fitting a curve through these data points. The first method being to draw a spline through the points using scipy.interpolate import spline

这导致(实际数据点绘制为蓝色标记):

This results in (with the actual data points drawn as blue markers):

这显然不好.

我的第二次尝试是使用 scipy.optimize import curve_fit 使用一系列不同阶的多项式绘制曲线拟合.即使是四阶多项式,答案也毫无用处(低阶多项式更没用):

My second attempt was to draw a curve fit using a series of different order polinimials using scipy.optimize import curve_fit. Even up to a fourth order polynomial the answer is useless (the lower order ones were even more useless):

最后,我使用 scipy.interpolate import interp1d 尝试在数据点之间进行插值.线性插值显然会产生预期的结果,但这条线是直的,这个练习的全部目的是获得一个很好的平滑曲线:

Finally, I used scipy.interpolate import interp1d to try and interpolate between the data points. Linear interpolation obviously yields expected results but the line are straight and the whole purpose of this exercise is to get a nice smooth curve:

如果我然后使用三次插值,我会得到一个糟糕的结果,但是二次插值会产生稍微好一点的结果:

If I then use cubic interpolation I get a rubish result, however quadratic interpolation yields a slightly better result:

但它还没有完全到位,而且我认为 interp1d 不能进行高阶插值.

But it's not quite there yet, and I don't think interp1d can do higher order interpolation.

有没有人有这样做的好方法?也许我最好尝试在 IPE 或其他东西中做这件事?

Is there anyone out there who has a good method of doing this? Maybe I would be better off trying to do it in IPE or something?

谢谢!

推荐答案

标准三次样条在间距非常不均匀的数据点之间进行外观合理的插值并不是很擅长.幸运的是,还有很多其他的插值算法,Scipy 提供了许多他们.以下是一些适用于您的数据的内容:

A standard cubic spline is not very good at reasonable looking interpolations between data points that are very unevenly spaced. Fortunately, there are plenty of other interpolation algorithms and Scipy provides a number of them. Here are a few applied to your data:

import numpy as np
from scipy.interpolate import spline, UnivariateSpline, Akima1DInterpolator, PchipInterpolator
import matplotlib.pyplot as plt

x_data = np.array([1, 1.2371, 1.6809, 2.89151, 5.13304, 9.23238])

y_data = np.array([0.0688824, 0.0490012, 0.0332843, 0.0235889, 0.0222304, 0.0245952])

x_data_smooth = np.linspace(min(x_data), max(x_data), 1000)
fig, ax = plt.subplots(1,1)

spl = UnivariateSpline(x_data, y_data, s=0, k=2)
y_data_smooth = spl(x_data_smooth)
ax.plot(x_data_smooth, y_data_smooth, 'b')

bi = Akima1DInterpolator(x_data, y_data)
y_data_smooth = bi(x_data_smooth)
ax.plot(x_data_smooth, y_data_smooth, 'g')

bi = PchipInterpolator(x_data, y_data)
y_data_smooth = bi(x_data_smooth)
ax.plot(x_data_smooth, y_data_smooth, 'k')

ax.plot(x_data_smooth, y_data_smooth)
ax.scatter(x_data, y_data)

plt.show()

我建议浏览这些内容以及其他一些内容,并找到与您认为正确的内容相匹配的内容.此外,您可能还想再采样一些点.例如,我认为 PCHIP 算法希望保持数据点之间的拟合单调,因此数字化您的最小点将很有用(无论您使用何种算法,这可能都是一个好主意).

I suggest looking through these, and also a few others, and finding one that matches what you think looks right. Also, though, you may want to sample a few more points. For example, I think the PCHIP algorithm wants to keep the fit monotonic between data points, so digitizing your minimum point would be useful (and probably a good idea regardless of the algorithm you use).

这篇关于不均匀间隔点之间的直观插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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