英文:
Alphashape and PolygonPatch: basic example doesn't work. Why?
问题
抱歉,我只会为您提供代码的翻译。以下是您提供的代码的翻译:
我尝试使用来自[此处](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/)的Alphashape包。
尽管我已经正确复制并粘贴了初始页面上的示例,但我遇到以下错误:
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第87行,在PolygonPatch
return PathPatch(PolygonPath(polygon), **kwargs)
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第62行,在PolygonPath
vertices = concatenate([
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py",第63行,在
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
我正在尝试的代码:
```python
import numpy as np
from descartes import PolygonPatch
import matplotlib.pyplot as plt
import alphashape
points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
(0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
fig, ax = plt.subplots()
ax.scatter(*zip(*points_2d))
alpha_shape = alphashape.alphashape(points_2d, 0.)
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))
plt.savefig(f"./test.png")
您知道这为什么不起作用吗?
非常感谢!
<details>
<summary>英文:</summary>
I am trying to use the package Alphashape from [here](https://alphashape.readthedocs.io/_/downloads/en/latest/pdf/).
Although I correctly copied and pasted the example in the initial pages, I get the following error:
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 87, in PolygonPatch
return PathPatch(PolygonPath(polygon), **kwargs)
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 62, in PolygonPath
vertices = concatenate([
File "/anaconda3/lib/python3.8/site-packages/descartes/patch.py", line 63, in <listcomp>
concatenate([asarray(t.exterior)[:, :2]] +
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
The code I am trying:
import numpy as np
from descartes import PolygonPatch
import matplotlib.pyplot as plt
import alphashape
points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
(0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
fig, ax = plt.subplots()
ax.scatter(*zip(*points_2d))
alpha_shape = alphashape.alphashape(points_2d, 0.)
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2))
plt.savefig(f"./test.png")
Do you why this is not working?
Many thanks!
</details>
# 答案1
**得分**: 2
从我所能看出,这个问题似乎是由于descartes中shapely的实现出现问题导致的。
我猜测shapely改变了如何处理多边形的外部,而descartes可能还没有更新。
我不知道这是否是最好的方法,但我直接编辑了我的descartes安装以修复这个问题:
1. 转到你的descartes安装目录并打开patch.py文件。
2. 在第62行,你应该看到这段代码:
```python
vertices = concatenate([
concatenate([asarray(t.exterior)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
for t in polygon])
只需将其中的"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])
英文:
So from what I could tell, this issue comes from a broken implementation of shapely within descartes.
My speculation is that shapely changed how it handles Polygon exteriors and descartes simply hasn't been updated.
I don't know if it is the best idea, but I edited my installation of descartes directly to fix this issue:
Navigate to your descartes installation and open patch.py.
At line 62 you should see this piece of code:
vertices = concatenate([
concatenate([asarray(t.exterior)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
for t in polygon])
Simply change t.exterior to t.exterior.coords. This hopefully should fix your issue.
vertices = concatenate([
concatenate([asarray(t.exterior.coords)[:, :2]] + [asarray(r)[:, :2] for r in t.interiors])
for t in polygon])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论