繁体   English   中英

匀称的颜色重叠多边形 Python

[英]Color Overlapping Polygons in Shapely Python

我正在使用 Shapely 中的一组重叠圆圈。 我试图弄清楚如何为我的结果列表中的每个圆圈片段着色。

这是我的代码:

import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString, Polygon, MultiPoint, MultiPolygon
from shapely.ops import unary_union, polygonize


def plot_coords(coords):
    pts = list(coords)
    x, y = zip(*pts)
    plt.plot(x,y)


def plot_polys(polys):
    for poly in polys:
        plot_coords(poly.exterior.coords)
        plt.fill_between(*poly.exterior.xy, alpha=.5)


points = [Point(0, 0),
             Point(2,0),
             Point(1,2),
             Point(-1,2),
             Point(-2,0),
             Point(-1,-2),
             Point(1,-2)]

# buffer points to create circle polygons

circles = []
for point in points:
    circles.append(point.buffer(2.25))

# unary_union and polygonize to find overlaps

rings = [LineString(list(pol.exterior.coords)) for pol in circles]
union = unary_union(rings)
result = [geom for geom in polygonize(union)]

# plot resulting polygons

plot_polys(result)
plt.show()

这是 plot:

在此处输入图像描述

在此示例中,由于所有重叠,由 2.25 缓冲的 7 个点导致总共 43 个多边形。 我想为 43 个段中的每一个段选择 colors。 结果是一个列表 object,所以我想知道是否可以为每个列表项添加颜色变量,或者是否需要在 plot_coords 或 plot_polys 函数中添加颜色。

我已经尝试从本教程中更改 plt.fill_between 行中的“facecolor”和“linewidth”,但它不能正常工作,所以我不确定颜色说明实际上来自哪里。

任何帮助将不胜感激!

我不知道这是否是您尝试做的,但在这里我为每个多边形分配一种颜色


import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString
from shapely.ops import unary_union, polygonize

from matplotlib.pyplot import cm
import numpy as np

def plot_coords(coords, color):
    pts = list(coords)
    x, y = zip(*pts)
    print(color)
    plt.plot(x,y, color=color)
    plt.fill_between(x, y, facecolor=color)


def plot_polys(polys, colors):
    for poly, color in zip(polys, colors):
        plot_coords(poly.exterior.coords, color)


points = [Point(0, 0),
             Point(2,0),
             Point(1,2),
             Point(-1,2),
             Point(-2,0),
             Point(-1,-2),
             Point(1,-2)]

# buffer points to create circle polygons

circles = []
for point in points:
    circles.append(point.buffer(2.25))

# unary_union and polygonize to find overlaps

rings = [LineString(list(pol.exterior.coords)) for pol in circles]
union = unary_union(rings)
result = [geom for geom in polygonize(union)]

# plot resulting polygons

colors = cm.rainbow(np.linspace(0, 1, len(result)))

plot_polys(result, colors)

plt.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM