python - 如何在函数的积分下绘制面积?参数多边形给出参数错误
问题描述
我正在尝试计算积分,绘制它然后突出显示它下面的区域。我设法找到了一些使用 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 新手。
解决方案
我认为两个图书馆之间存在混淆。您尝试使用 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”。
推荐阅读
- mongodb - 弹性搜索模糊搜索根和嵌套字段
- python - python的分数
- javascript - 尝试在 Angular 9 中使用共享模块时出错,使我的组件无法识别我的材料模块
- swift - 有没有办法显示一个较大的数字在一个较小的数字范围内的位置?
- asp.net - 在电子邮件正文中添加多个图像作为内联附件
- python - 从 Numpy Array 添加数据框的顶行
- graphql - 在graphql中嵌套数据的正确方法是什么?
- c++ - 为什么以下两种解决方案的时间复杂度如此不同?
- javascript - Javascript滚动动画切断页面
- python - 尝试在 Python 中插入直方图函数的输出