如何用三次样条或 pchip 插值 semilogx 图
How to interpolate semilogx plot with cubic spline or pchip
所以我已经坚持了一段时间。我想知道如何使用 pchip 或三次样条等不同方法在 semilogx 图上进行插值。到目前为止,这是我拥有的代码。
from scipy.interpolate import PchipInterpolator
import numpy as np
import matplotlib.pyplot as plt
data = [[0.425, 100],
[0.18, 96],
[0.090, 85],
[0.075, 80],
[0.04, 59],
[0.02, 39],
[0.01, 26],
[0.005, 15],
[0.0015, 8]]
data = np.array(data)
x = data[:, 0]
y = data[:, 1]
x = np.flip(x)
y = np.flip(y)
interp_obj = PchipInterpolator(x, y)
new_x_vals = np.arange(0.0015, 0.42501, 0.0001)
new_y_vals = interp_obj(new_x_vals)
plt.semilogx(new_x_vals, new_y_vals)
plt.xlabel("Particle Diameter (mm) - Log Scale")
plt.ylabel("Percent Finer")
plt.show()
好像这不是应该的那么顺利。我应该在插值之前转换数据吗?
Should I be transforming the data before interpolation?
是的,当然!您必须确保样本是对数 spaced。对数轴上的线性 spaced 个样本在图的右侧挤在一起,在左侧拉开。
此外,我认为 log-x-plot 使三次插值在 log space 中看起来是线性的,这就是您实际观察到的效果。
如果在插值之前对 x 值进行对数变换
interp_obj = PchipInterpolator(np.log10(x), y)
new_x_vals = np.arange(0.0015, 0.42501, 0.0001)
new_y_vals = interp_obj(np.log10(new_x_vals))
你得到这个结果:
所以我已经坚持了一段时间。我想知道如何使用 pchip 或三次样条等不同方法在 semilogx 图上进行插值。到目前为止,这是我拥有的代码。
from scipy.interpolate import PchipInterpolator
import numpy as np
import matplotlib.pyplot as plt
data = [[0.425, 100],
[0.18, 96],
[0.090, 85],
[0.075, 80],
[0.04, 59],
[0.02, 39],
[0.01, 26],
[0.005, 15],
[0.0015, 8]]
data = np.array(data)
x = data[:, 0]
y = data[:, 1]
x = np.flip(x)
y = np.flip(y)
interp_obj = PchipInterpolator(x, y)
new_x_vals = np.arange(0.0015, 0.42501, 0.0001)
new_y_vals = interp_obj(new_x_vals)
plt.semilogx(new_x_vals, new_y_vals)
plt.xlabel("Particle Diameter (mm) - Log Scale")
plt.ylabel("Percent Finer")
plt.show()
好像这不是应该的那么顺利。我应该在插值之前转换数据吗?
Should I be transforming the data before interpolation?
是的,当然!您必须确保样本是对数 spaced。对数轴上的线性 spaced 个样本在图的右侧挤在一起,在左侧拉开。
此外,我认为 log-x-plot 使三次插值在 log space 中看起来是线性的,这就是您实际观察到的效果。
如果在插值之前对 x 值进行对数变换
interp_obj = PchipInterpolator(np.log10(x), y)
new_x_vals = np.arange(0.0015, 0.42501, 0.0001)
new_y_vals = interp_obj(np.log10(new_x_vals))
你得到这个结果: