如何找到以非单调顺序通过固定数量点的多项式?

发布于 2025-01-20 08:16:41 字数 850 浏览 1 评论 0 原文

我有不同对象的 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

enter image description here

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:

example of what I want

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文