我有不同对象的 X 和 Y 坐标列表(最多 5 个对象,最少 0 个)。我想绘制一个平滑的多项式,以正确的顺序连接它们。到目前为止我看到的所有示例都要求 X 值严格按递增顺序排列。我的尝试之一如下:
import numpy as np
import matplotlib.pyplot as plt
xlist = [4, 5, 4.5] # x-coordinates
ylist = [4,7,10] # y-coordinates
z = np.polyfit(xlist, ylist, 2)
f = np.poly1d(z)
xx = np.linspace(np.array(xlist).min(),np.array(xlist).max(),100)
plt.plot(xlist, ylist, 'ob')
plt.plot(xx, f(xx))
plt.show
这个结果当然不是我想要的,因为顺序被忽略了。
我也研究过使用三次样条,但在顺序上面临类似的问题。
编辑:这是我希望的结果:
I have lists of X and Y coordinates of different objects (up to 5 objects maximum, 0 minimum). I want to plot a smooth polynomial connecting them in their correct order. All the examples I have seen so far require the X-values to be in strictly increasing order. One of my attempts is as follows:
import numpy as np
import matplotlib.pyplot as plt
xlist = [4, 5, 4.5] # x-coordinates
ylist = [4,7,10] # y-coordinates
z = np.polyfit(xlist, ylist, 2)
f = np.poly1d(z)
xx = np.linspace(np.array(xlist).min(),np.array(xlist).max(),100)
plt.plot(xlist, ylist, 'ob')
plt.plot(xx, f(xx))
plt.show
This result is of course not what I wanted, as the order is ignored.
I have also looked into using cubic splines but facing a similar issue with the order.
Edit: Here is what I want my result to look like:
发布评论