使用匀称的descart和matplotlib绘制断开连接的实体

时间:2014-12-19 22:40:26

标签: python matplotlib shapely descartes

我需要绘制一个断开连接的圆圈列表,这些圆圈是我为其他目的而形成的。

我试图完全按照http://toblerity.org/shapely/manual.html#cascading-unions节目中的示例(参见code)进行操作,但只有当圆圈重叠并且整体事物已连接时才有效(这不是我的情况)。正如您所看到的那样,通过替换

polygons = [Point(i, 0).buffer(0.7) for i in range(5)]

polygons = [Point(i, 0).buffer(0.7) for i in (0,4)]

打破了AssertionError因为笛卡尔不是多边形的东西(或者如果一个人将笛卡尔断言作为测试注释掉,那么matplotlib会失败assert vertices.ndim == 2

查看matplotlib.path.Path的文档,似乎可以使用MOVETO来实现这一目标,但似乎并不支持它。它是否正确?我有什么变通方法?

1 个答案:

答案 0 :(得分:4)

以下代码有效:

from shapely.ops import cascaded_union
from shapely.geometry import Point
import random
from matplotlib.patches import Polygon
import pylab as pl
import numpy as np

circles = [Point(random.random(), random.random()).buffer(random.random() * 0.1) 
            for i in range(100)]

polygons = cascaded_union(circles)

fig, ax = pl.subplots(figsize=(8, 8))

for polygon in polygons:
    mpl_poly = Polygon(np.array(polygon.exterior), facecolor="g", lw=0, alpha=0.4)
    ax.add_patch(mpl_poly)

ax.relim()
ax.autoscale()

输出:

enter image description here

相关问题