在python中,绘图时如何用平滑的线连接点?
in python, how to connect points with smooth line in plotting?
我正在尝试使用样条曲线绘制点 + 平滑线。但是 "overshoots" 行有些点,例如在下面的代码中,超过了点 0.85。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline
x=np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y=np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(),500)
y_smooth = spline(x, y, x_new)
plt.plot (x_new,y_smooth)
plt.scatter (x, y)
我该如何解决?
您可以尝试在 scipy.interpolate:
中使用 interp1d
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x=np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y=np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(),500)
f = interp1d(x, y, kind='quadratic')
y_smooth=f(x_new)
plt.plot (x_new,y_smooth)
plt.scatter (x, y)
产生:
kind
参数的一些其他选项在文档中:
kind : str or int, optional Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’ where ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order) or as an integer specifying the order of the spline interpolator to use. Default is ‘linear’.
您也可以尝试 thin plate spline 使用来自 scipy 的径向基函数插值:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
x = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y = np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(), 500)
rbf = Rbf(x, y, function = 'thin_plate', smooth = 0.001)
y_smooth = rbf(x_new)
plt.plot(x_new, y_smooth)
plt.scatter (x, y);
可以通过改变 smooth
参数来获得更好的数据近似值。
要考虑的替代 function
参数值包括 'multiquadric'、'inverse'、'gaussian'、'linear'、'cubic' 和 'quintic'.在考虑 function
值时,我通常先尝试 'thin_plate',然后再尝试 'cubic'。
检查 scipy.interpolate.Rbf docs 中的其他 Rbf 选项。
我正在尝试使用样条曲线绘制点 + 平滑线。但是 "overshoots" 行有些点,例如在下面的代码中,超过了点 0.85。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline
x=np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y=np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(),500)
y_smooth = spline(x, y, x_new)
plt.plot (x_new,y_smooth)
plt.scatter (x, y)
我该如何解决?
您可以尝试在 scipy.interpolate:
中使用 interp1dimport numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x=np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y=np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(),500)
f = interp1d(x, y, kind='quadratic')
y_smooth=f(x_new)
plt.plot (x_new,y_smooth)
plt.scatter (x, y)
产生:
kind
参数的一些其他选项在文档中:
kind : str or int, optional Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’ where ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order) or as an integer specifying the order of the spline interpolator to use. Default is ‘linear’.
您也可以尝试 thin plate spline 使用来自 scipy 的径向基函数插值:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import Rbf
x = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2])
y = np.array([0.57,0.85,0.66,0.84,0.59,0.55,0.61,0.76,0.54,0.55,0.48])
x_new = np.linspace(x.min(), x.max(), 500)
rbf = Rbf(x, y, function = 'thin_plate', smooth = 0.001)
y_smooth = rbf(x_new)
plt.plot(x_new, y_smooth)
plt.scatter (x, y);
可以通过改变 smooth
参数来获得更好的数据近似值。
要考虑的替代 function
参数值包括 'multiquadric'、'inverse'、'gaussian'、'linear'、'cubic' 和 'quintic'.在考虑 function
值时,我通常先尝试 'thin_plate',然后再尝试 'cubic'。
检查 scipy.interpolate.Rbf docs 中的其他 Rbf 选项。