[英]Color Overlapping Polygons in Shapely Python
I am working with a set of overlapping circles in Shapely.我正在使用 Shapely 中的一组重叠圆圈。 I am trying to figure out how to color each circle fragment in my results list.
我试图弄清楚如何为我的结果列表中的每个圆圈片段着色。
Here's my code:这是我的代码:
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()
Here's the plot:这是 plot:
In this example, 7 points buffered by 2.25 results in a total of 43 polygons due to all of the overlap.在此示例中,由于所有重叠,由 2.25 缓冲的 7 个点导致总共 43 个多边形。 I want to choose the colors for each of the 43 segments.
我想为 43 个段中的每一个段选择 colors。 Results is a list object, so I am wondering if I can add a variable for color to each list item, or if I need to add the color in the plot_coords or plot_polys functions.
结果是一个列表 object,所以我想知道是否可以为每个列表项添加颜色变量,或者是否需要在 plot_coords 或 plot_polys 函数中添加颜色。
I have tried changing the "facecolor" and "linewidth" in the plt.fill_between line, from this tutoria l, but it isn't working right, so I'm unsure where the instructions for color are actually coming from.我已经尝试从本教程中更改 plt.fill_between 行中的“facecolor”和“linewidth”,但它不能正常工作,所以我不确定颜色说明实际上来自哪里。
Any help would be greatly appreciated!任何帮助将不胜感激!
I don't know if this is what you tried to do, but here I assign one color to every Polygon我不知道这是否是您尝试做的,但在这里我为每个多边形分配一种颜色
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.