我有两条曲线的x
和y
值的列表,它们都具有奇怪的形状,并且我没有任何一个曲线的函数。我需要做两件事:
- 绘制这些曲线并填充它们之间的阴影区域,就像下面的图片一样。
- 找到这些被填充的曲线之间的阴影区域的总面积。
我能够使用matplotlib
中的fill_between
和fill_betweenx
绘制并填充这些曲线之间的区域,但我不知道如何计算它们之间的确切面积,特别是因为我没有这些曲线的任何函数。
有什么想法吗?
我已经搜索了所有地方,但找不到简单的解决方法。我非常绝望,所以非常感谢任何帮助。
非常感谢!
编辑:供以后参考(如果有人遇到同样的问题),这是我解决的方法:连接每条曲线的第一个和最后一个节点/点,得到一个大的奇怪形状的多边形,然后使用shapely自动计算多边形的面积,这是曲线之间的确切面积,无论它们朝哪个方向或者有多么非线性。效果惊人! :)
这是我的代码:
from shapely.geometry import Polygon
x_y_curve1 = [(0.121,0.232),(2.898,4.554),(7.865,9.987)] #these are your points for curve 1 (I just put some random numbers)
x_y_curve2 = [(1.221,1.232),(3.898,5.554),(8.865,7.987)] #these are your points for curve 2 (I just put some random numbers)
polygon_points = [] #creates a empty list where we will append the points to create the polygon
for xyvalue in x_y_curve1:
polygon_points.append([xyvalue[0],xyvalue[1]]) #append all xy points for curve 1
for xyvalue in x_y_curve2[::-1]:
polygon_points.append([xyvalue[0],xyvalue[1]]) #append all xy points for curve 2 in the reverse order (from last point to first point)
for xyvalue in x_y_curve1[0:1]:
polygon_points.append([xyvalue[0],xyvalue[1]]) #append the first point in curve 1 again, to it "closes" the polygon
polygon = Polygon(polygon_points)
area = polygon.area
print(area)
编辑 2:感谢回答。就像 Kyle 解释的那样,这只适用于正值。如果您的曲线低于0(这不是我的情况,如示例图表所示),那么您将不得不使用绝对数值。