【问题标题】:Create a third curve from other two using python使用 python 从其他两条曲线创建第三条曲线
【发布时间】:2021-12-30 17:23:23
【问题描述】:

我有两个 xy 坐标列表,用于绘制两条曲线。我对曲线上方的区域感兴趣,所以我使用了 fill_between 来得出这个结论:

现在,我想要的是一种获取颜色区域未覆盖的坐标的方法,因此我可以绘制第三条曲线,就像我在下面的示例中使用 Paint 所做的红色曲线一样:

我尝试对列表进行排序,然后比较每一对以找到具有较低 y 值的列表,但这不起作用,因为每个列表可以有不同的大小和不同的 x 值。我还发现了一些关于叉积的线程,但这些线程使用的是直线,我无法理解如何将其推断到我的情况。

这是一个mwe:

import matplotlib.pyplot as plt

points_x = [1,3,5,7,9,11,13,15,17,19]
points_y = [10,8,7,6,3,5,3,9,7,6]
points_x2= [0,2,4,8,10,12,14,15,17,19,20,21,22]
points_y2 = [12,10,9,4,2,7,3,8,8,8,8,8,8]

plt.scatter(points_x, points_y, color="blue")
plt.scatter(points_x2, points_y2, color="green")

plt.fill_between(points_x, points_y, plt.ylim()[1], color='blue', alpha=0.5)
plt.fill_between(points_x2, points_y2, plt.ylim()[1], color='green', alpha=0.5)

#way to make a points_x3/points_y3 with only the coordinates at the edge of the 
#overlapped areas

plt.show()

(我在想出正确的术语来定义我的问题时遇到了麻烦(非母语人士并且不习惯使用太多数学术语),因此对于可能提出重复问题的问题我深表歉意)

【问题讨论】:

  • 您在 x=15 附近错过了一点吗?
  • @MadPhysicist 在第二个情节中?是的,我做到了,我的错。谢天谢地,我仍然得到了答案。

标签: python matplotlib plot curve


【解决方案1】:

您可以将两条曲线组织到一个共同的 x 轴上,然后计算它们的最小值:

import matplotlib.pyplot as plt
import numpy as np

points_x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
points_y = [10, 8, 7, 6, 3, 5, 3, 9, 7, 6]
points_x2 = [0, 2, 4, 8, 10, 12, 14, 15, 17, 19, 20, 21, 22]
points_y2 = [12, 10, 9, 4, 2, 7, 3, 8, 8, 8, 8, 8, 8]

plt.scatter(points_x, points_y, color="blue")
plt.scatter(points_x2, points_y2, color="green")

plt.fill_between(points_x, points_y, plt.ylim()[1], color='blue', alpha=0.5)
plt.fill_between(points_x2, points_y2, plt.ylim()[1], color='green', alpha=0.5)

# way to make a points_x3/points_y3 with only the coordinates at the edge of the
# overlapped areas

x_long = np.unique(np.append(points_x, points_x2))
y_long = np.interp(x_long, points_x, points_y, left=np.nan, right=np.nan)
y2_long = np.interp(x_long, points_x2, points_y2, left=np.nan, right=np.nan)
y_min = np.nanmin([y_long, y2_long], axis=0)

plt.plot(x_long, y_min, color='red', ls='--', alpha=0.5, lw=4)
plt.show()

【讨论】:

  • 谢谢,伙计。将您的解决方案应用于我的实际代码使我意识到我需要一些稍微不同的东西,但现在我可以从这里获取它。
【解决方案2】:

如果我理解正确,您想要的称为凸包,您可以使用 scipy 进行计算:

from scipy.spatial import ConvexHull

points_x = [1,3,5,7,9,11,13,15,17,19]
points_y = [10,8,7,6,3,5,3,9,7,6]
points_x2= [0,2,4,8,10,12,14,15,17,19,20,21,22]
points_y2 = [12,10,9,4,2,7,3,8,8,8,8,8,8]

# making array of x/y coordinates
a = np.array([points_x+points_x2, points_y+points_y2]).T

min_x = a[:,0].min()
max_x = a[:,0].max()
max_y = a[:,1].max()+1 # arbitrary +1 for ylim

# concatenate extra points to array
a = np.r_[a,np.array([[min_x, max_y], [max_x, max_y]])]

# compute the convex hull
hull = ConvexHull(a)

plt.scatter(points_x, points_y, color="blue")
plt.scatter(points_x2, points_y2, color="green")

plt.fill_between(points_x, points_y, plt.ylim()[1], color='blue', alpha=0.5)
plt.fill_between(points_x2, points_y2, plt.ylim()[1], color='green', alpha=0.5)

idx = np.r_[hull.vertices,hull.vertices[0]]

plt.plot(a[idx,0], a[idx,1], 'r', lw=2)

输出:

【讨论】:

  • 虽然这可能是 OP “真正”之后的内容,但这也不是问题所在。
  • @MadPhysicist 我知道,我在发布后意识到这个例子是模棱两可的,有些顶点看起来像凸包,有些似乎是最小值。我想我们会看到的。希望 OP 对任何一个答案都满意;)
  • 我接受了另一个答案,但感谢您让我了解凸包。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2021-10-13
  • 2021-02-23
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多