python - 使用 python 从另外两条曲线创建第三条曲线

标签 python matplotlib plot curve

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

enter image description here

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

enter image description here

我尝试对列表进行排序,然后比较每一对以查找 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 轴上,然后计算它们的最小值:

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()

minimum of two curves

关于python - 使用 python 从另外两条曲线创建第三条曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70534796/

相关文章:

python - Pandas "TypeError: unsupported operand type(s) for +: ' Timedelta' 和 'float' "

matlab - 绘制日志(n 超过 k)

r - 在函数中使用可选参数 (...),如新人口金字塔图所示

python - 如何在 Jupyter 笔记本中使用 rpy2 绘制(内联)?

python - 如何使用线条和标记绘制重叠系列?

Python pygame事件循环类型错误

python - 如何在更改列名时正确读取 Pandas 中的 csv

python - 自身的类子类。为什么禁止相互子类化?

Python:登录后无法删除文件

python - 绘制一个圆-matplotlib