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

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

问题描述

我有以下图表,希望使用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使用一系列不同阶的polinimials绘制曲线拟合.即使是四阶多项式,答案也无济于事(低阶多项式甚至无济于事):

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天全站免登陆