如何平滑地连接多边形顶点之间的直线?

3
我想要通过我的散点绘制平滑的线条。椭圆无法适配,所以我画了一个多边形,但是我无法用多边形获得平滑的线条。我也尝试过PathPatch,但是这样线条就不会经过散点。有没有办法强制PathPatch通过散点?
我添加了一个示例,希望能说明我的意图。我不想要黑色线条的硬边缘。
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse, Polygon

ax = plt.subplot()
ax.set_aspect("equal")
ax.add_artist(Ellipse((0, 0), 10, 5, fill=False, color='green'))

plt.scatter([5, -6, 0, 0], [0, 0, 2.5, -4], marker='X')
ax.add_artist(Polygon([[5, 0], [0, 2.5], [-6, 0], [0, -4]], closed=True, fill=False))

plt.xlim((-6, 6))
plt.ylim((-6, 6))
plt.show()

enter image description here

1个回答

2
此答案所述,您可以使用封闭样条插值法来插值数据点,为此您可以使用scipy.interpolate.splprepscipy.interpolate.splev。您首先需要将xy的第一个元素追加到末尾以关闭样条线。
import matplotlib.pyplot as plt
from scipy.interpolate import splprep, splev
import numpy as np


x = [0, 5, 0, -6]
y = [-4, 0, 2, 0]

x.append(x[0])
y.append(y[0])

tck, _ = splprep([x, y], s = 0, per = True)
xx, yy = splev(np.linspace(0, 1, 100), tck, der = 0)


fig, ax = plt.subplots()

ax.scatter(x, y, marker = 'X')
ax.plot(xx, yy, 'green')

plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接