绘制和返回从数据帧绘制的两条曲线之间的交点

问题描述

我是编码新手,目前正在尝试找出如何找出从数据帧绘制的两条曲线的交点。

曲线是:

here

这是我现在拥有的代码

GZ_positive = data.loc[data["GZ"] >= 0] 

x_new = GZ_positive['heel']
y_new = GZ_positive['GZ']


wind_heeling = pd.read_csv('wind_heeling.csv')  

GZ_positive['GZ'] = GZ_positive['GZ']/13400
wind_heeling['GZ_wind'] = wind_heeling['GZ_wind']/13400

x_wind = wind_heeling['Heel']
y_wind = wind_heeling['GZ_wind']

plt.figure()
plt.plot(x_new,y_new)
plt.plot(x_wind,y_wind)
plt.grid(True)
plt.show()

然后我想在交点之前和之后删除橙色线的所有点。此分析的目的是计算橙色图形下方但与蓝色图形相交的点之间的面积:

here

在网上发现了一些类似的问题,但似乎没有任何效果(或者我做错了)。如果有人能指出我正确的方向,我将不胜感激。谢谢!

解决方法

您可以定义两条新曲线。一条由两条曲线的逐点最小值给出,另一条将是零处的水平线。现在,您可以使用 fill_between 中的 matplotlib 方法。下面是一个例子:

xs = np.linspace(0,10,100)
ys = -xs**2 - 4 + 10*xs

df = pd.DataFrame({
    "one": 5,"two": ys,})

ax = df.plot()
ax.set_ylim(0,30)

upper = df.min(1)
lower = np.zeros(df.shape[0])

ax.fill_between(df.index,lower,upper)

enter image description here