Abi*_*ash 3 python matplotlib shapely descartes
我曾经使用 shapely 制作一个圆圈并将其绘制在之前填充的图上。这曾经工作得很好。最近,我收到索引错误。我将代码分解为最简单的操作,但它甚至无法执行最简单的循环。
import descartes
import shapely.geometry as sg
import matplotlib.pyplot as plt
circle = sg.Point((0,0)).buffer(1)
# Plot the cricle
fig = plt.figure()
ax = fig.add_subplot(111)
patch = descartes.PolygonPatch(circle)
ax.add_patch(patch)
plt.show()
Run Code Online (Sandbox Code Playgroud)
以下是我现在遇到的错误。我觉得这可能是新版本与可能发生的事情不匹配。我尝试卸载并重新安装最后一个已知的稳定版本,但这也没有帮助
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[20], line 6
4 fig = plt.figure()
5 ax = fig.add_subplot(111)
----> 6 patch = descartes.PolygonPatch(circle)
7 ax.add_patch(patch)
8 plt.show()
File ~/env/lib/python3.8/site-packages/descartes/patch.py:87, in PolygonPatch(polygon, **kwargs)
73 def PolygonPatch(polygon, **kwargs):
74 """Constructs a matplotlib patch from a geometric object
75
76 The `polygon` may be a Shapely or GeoJSON-like object with or without holes.
(...)
85
86 """
---> 87 return PathPatch(PolygonPath(polygon), **kwargs)
File ~/env/lib/python3.8/site-packages/descartes/patch.py:62, in PolygonPath(polygon)
58 else:
59 raise ValueError(
60 "A polygon or multi-polygon representation is required")
---> 62 vertices = concatenate([
63 concatenate([asarray(t.exterior)[:, :2]] +
64 [asarray(r)[:, :2] for r in t.interiors])
65 for t in polygon])
66 codes = concatenate([
67 concatenate([coding(t.exterior)] +
68 [coding(r) for r in t.interiors]) for t in polygon])
70 return Path(vertices, codes)
File ~/env/lib/python3.8/site-packages/descartes/patch.py:63, in <listcomp>(.0)
58 else:
59 raise ValueError(
60 "A polygon or multi-polygon representation is required")
62 vertices = concatenate([
---> 63 concatenate([asarray(t.exterior)[:, :2]] +
64 [asarray(r)[:, :2] for r in t.interiors])
65 for t in polygon])
66 codes = concatenate([
67 concatenate([coding(t.exterior)] +
68 [coding(r) for r in t.interiors]) for t in polygon])
70 return Path(vertices, codes)
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
Run Code Online (Sandbox Code Playgroud)
Jac*_*cob 12
因此,据我所知,这个问题来自shapelyinside的错误实现descartes。
我的猜测是,它shapely改变了它处理多边形外部的方式,并且descartes根本没有更新。
我不知道这是否是最好的主意,但我descartes直接编辑了我的安装来解决这个问题:
导航到您的descartes安装并打开patch.py.
在第 62 行,您应该看到这段代码:
vertices = concatenate([
concatenate([asarray(t.exterior)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
for t in polygon])
Run Code Online (Sandbox Code Playgroud)
只需更改t.exterior为t.exterior.coords. 这有望解决您的问题。
vertices = concatenate([
concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
for t in polygon])
Run Code Online (Sandbox Code Playgroud)
我正在尝试找到一种方法向descartes开发人员提供此反馈。
| 归档时间: |
|
| 查看次数: |
2116 次 |
| 最近记录: |