[英]in python, how to connect points with smooth line in plotting?
I am trying to plot points + smooth line using spline. 我试图使用样条曲线绘制点+平滑线。 But the line "overshoots" some points, eg in following codes, over the point 0.85.
但该线“超过”某些点,例如在以下代码中,超过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)
how do I fix it? 我如何解决它?
You might try the using interp1d in scipy.interpolate: 您可以尝试在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)
which yields: 产量:
some other options for the kind
parameter are in the docs: 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.
kind:str或int,optional指定插值的类型为字符串('linear','nearest','zero','slinear','quadratic','cubic',其中'zero','slinear','quadratic '和'cubic'指的是第零,第一,第二或第三阶的样条插值)或指定要使用的样条插值器的阶数的整数。 Default is 'linear'.
默认为“线性”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.