简体   繁体   English

对于较大的 x/y 值,有没有办法在两点之间实现平滑曲线?

[英]Is there a way to achieve a smooth curve between two points for larger x/y values?

I have been trying to create a slight curve between two points using Python and Matplotlib. I found a method on here that does apply a curve to the line between two points but not in the way I expected/was hoping for.我一直在尝试使用 Python 和 Matplotlib 在两点之间创建一条轻微的曲线。我在这里找到了一种方法,它确实将曲线应用于两点之间的直线,但不是我预期/希望的方式。 Below is the code I am currently using to draw the curved line.下面是我目前用来绘制曲线的代码。

Current graph: Currently what line looks like当前图表:当前线的样子

Desired graph: Desired curved line所需图形:所需曲线

Currently it is almost a 90 deg.目前它几乎是90度。 angle but I am trying to achieve a more even curve.角度,但我正在努力获得更均匀的曲线。 This solution does seem to achieve the desired results for smaller values, but when using larger values for the x/y coords this is what I am seeing.对于较小的值,此解决方案似乎确实达到了预期的结果,但是当对 x/y 坐标使用较大的值时,这就是我所看到的。

p1 = [125, -203]
p2 = [49, -75]

plt.plot(p1[0], p1[1], marker="o", markersize=10, color="black", label="P1")
plt.plot(p2[0], p2[1], marker="o", markersize=10, color="red", label="P1")

plt.plot((p1[0], p2[0]),
         (p1[1], p2[1]),
         linewidth=5,
         label="Straight line")

x, y = draw_curve(p1, p2)
plt.plot(x, y, linewidth=5, label="Curved line", color="orange")
plt.show()


def draw_curve(p1, p2):
a = (p2[1] - p1[1]) / (np.cosh(p2[0]) - np.cosh(p1[0]))
b = p1[1] - a * np.cosh(p1[0])
x = np.linspace(p1[0], p2[0], 100)
y = a * np.cosh(x) + b

return x, y

There are infinitely many ways to come up with a curve.有无数种方法可以得出曲线。 One way to come up with a curve that you can manually tune (the "angle") is to add a third point where you would like the curve to pass through:想出一条可以手动调整的曲线(“角度”)的一种方法是在您希望曲线通过的位置添加第三个点:

p1 = [125, -203]
p2 = [49, -75]
p3 = [90, -100] # the third point

plt.plot(p1[0], p1[1], marker="o", markersize=10, color="black", label="P1")
plt.plot(p2[0], p2[1], marker="o", markersize=10, color="red", label="P1")

plt.plot((p1[0], p2[0]),
         (p1[1], p2[1]),
         linewidth=5,
         label="Straight line")

def draw_curve(p1, p2, p3):
    f = np.poly1d(np.polyfit((p1[0], p2[0], p3[0]), (p1[1], p2[1], p3[1]), 2))
    x = np.linspace(p1[0], p2[0], 100)
    return x, f(x)

x, y = draw_curve(p1, p2, p3)
plt.plot(x, y, linewidth=5, label="Curved line", color="orange")
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM