【问题标题】:Computing the area filled by matplotlib.pyplot.fill(...)计算由 matplotlib.pyplot.fill(...) 填充的区域
【发布时间】:2020-06-17 10:56:56
【问题描述】:
我想计算由两个向量 a 和 b 定义的曲线内部的面积。供您参考,曲线看起来像这样 (pyplot.plot(a,b)):
我看到 matplotlib 有一个fill 功能,可以让你填充曲线包围的区域:
我想知道,有什么方法可以使用相同的功能获得填充的区域?这将非常有用,因为我认为计算该区域的另一种方法是通过数值积分,更加麻烦。
感谢您的宝贵时间。
【问题讨论】:
标签:
python-3.x
matplotlib
【解决方案1】:
如果你真的想找到被matplotlib.pyplot.fill(a, b)填充的区域,你可以使用它的输出如下:
def computeArea(pos):
x, y = (zip(*pos))
return 0.5 * numpy.abs(numpy.dot(x, numpy.roll(y, 1)) - numpy.dot(y, numpy.roll(x, 1)))
# pyplot.fill(a, b) will return a list of matplotlib.patches.Polygon.
polygon = matplotlib.pyplot.fill(a, b)
# The area of the polygon can be computed as follows:
# (you could also sum the areas of all polygons in the list).
print(computeArea(polygon[0].xy))
此方法基于this answer,
而且它不是最有效的。