Dav*_*ide 3 python matplotlib shapely descartes
我需要绘制一个不连贯的圆的列表,这些圆是我为其他目的以匀称的方式创建的。
我试图完全按照http://toblerity.org/shapely/manual.html#cascading-unions中的示例显示(见代码),但只有当圆圈重叠并且整体连接时才有效(这不是在我的情况下)。正如您可以通过替换该行看到的
polygons = [Point(i, 0).buffer(0.7) for i in range(5)]
Run Code Online (Sandbox Code Playgroud)
和
polygons = [Point(i, 0).buffer(0.7) for i in (0,4)]
Run Code Online (Sandbox Code Playgroud)
与和休息AssertionError的事情不是由笛卡尔是一个多边形(或matplotlib失败assert vertices.ndim == 2,如果一个评论了笛卡尔断言作为测试)
查看matplotlib.path.Path的文档,似乎可以使用 MOVETO 来实现此目标,但 shapely 似乎不支持它。这样对吗?我有哪些解决方法?
以下代码有效:
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()
Run Code Online (Sandbox Code Playgroud)
输出:

| 归档时间: |
|
| 查看次数: |
2765 次 |
| 最近记录: |