Plot Smooth Curve With Duplicate Values In List
I would like to display the smoothed curve between two lists. The two lists have values that they correspond to different values on the other list. The values of the two lists are
Solution 1:
You can parameterize a curve represented by the x/y
values with a parameter (called param
in the code below) that goes from 0 to 1 (or any other arbitary range), compute the coefficients of the interpolating spline and then interpolate the curve again using a finer spacing of the parameter.
param = np.linspace(0, 1, x.size)
spl = make_interp_spline(param, np.c_[x,y], k=2) #(1)
xnew, y_smooth = spl(np.linspace(0, 1, x.size * 100)).T #(2)
plt.plot(xnew, y_smooth)
plt.scatter(x, y, c="r")
Remarks:
The degree of the spline can be chosen to be greater than 2 if there are more than three datapoints available. The default would be k=3.
100 controls the amount of interpolated points for the new curve. If
x
becomes larger, a smaller number could be used here.
Result:
Post a Comment for "Plot Smooth Curve With Duplicate Values In List"