首页 > 解决方案 > 如何在函数的积分下绘制面积?参数多边形给出参数错误

问题描述

我正在尝试计算积分,绘制它然后突出显示它下面的区域。我设法找到了一些使用 Polygon 函数的代码,这给了我一个错误。

下面我提供一些代码。

a = 0.5 # left integration limit 
b = 9.5  # right integration limit
Ix = np.linspace(a, b)  
Iy = f(Ix) # Integration function values

verts = [(a, 0)] + list(zip(Ix, Iy)) + [(b, 0)]
poly = Polygon(verts, facecolor='0.7', edgecolor='0.5')

代码给了我以下错误,

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-0113439d2980> in <module>()
      5 
      6 verts = [(a, 0)] + list(zip(Ix, Iy)) + [(b, 0)]
----> 7 poly = Polygon(verts, facecolor='0.7', edgecolor='0.5')

TypeError: __init__() got an unexpected keyword argument 'facecolor'

我试图查看 shapely 用户手册https://shapely.readthedocs.io/en/latest/manual.html但无法找到解决方案。

我也发现了类似的错误,但这是关于 matplotlib。

抱歉,如果这是一个简单的问题,我是 python 新手。

标签: python

解决方案


我认为两个图书馆之间存在混淆。您尝试使用 Shapely,但该库的 Polygon 类没有“facecolor”参数:

请参阅文档的这一部分:

https://shapely.readthedocs.io/en/latest/manual.html#polygons

也许你以前使用过 cartopy 库,它有这个 facecolor 参数:

https://scitools.org.uk/cartopy/docs/v0.14/matplotlib/feature_interface.html

所以你应该删除这个论点:

a = 0.5 # left integration limit 
b = 9.5  # right integration limit
Ix = np.linspace(a, b)  
Iy = f(Ix) # Integration function values

verts = [(a, 0)] + list(zip(Ix, Iy)) + [(b, 0)]
poly = Polygon(verts)

或者使用 cartopy 库。

**来自问题的作者评论**:

实际上,导入必须从 Shapely Library 更改为“from matplotlib.patches import Polygon”。


推荐阅读