matplotlib 阿尔法形状和多边形面片:一个基本的例子是行不通的,为什么?

8cdiaqws  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在尝试使用来自here的软件包Alphashape。
尽管我在初始页面中正确地复制并粘贴了示例,但还是出现了以下错误:

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

我正在尝试的代码:

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")

你知道为什么这行不通吗?
非常感谢!

ezykj2lf

ezykj2lf1#

因此,据我所知,这个问题来自于笛卡尔对shapely的错误实现。
我的猜测是,shapely改变了它处理多边形外观的方式,笛卡尔根本没有更新。
我不知道这是否是最好的主意,但我直接编辑了我的笛卡尔安装来解决这个问题:

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])

只需将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])

相关问题