【发布时间】:2020-02-01 15:39:21
【问题描述】:
假设我们有一个 100x100 的网格,其中包含一个多边形。 现在,如果我们为多边形中包含的所有可能的 (x,y) 点 [x,y 是整数] 着色,我们应该期望多边形在某种程度上被绘制/填充
但我得到的图像永远不会正确地落入并填充多边形!这是身材的限制还是我做错了什么?! (请注意,我需要它用于其他目的,而不仅仅是绘制多边形)
polygon and filled area not overlapping
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
points = np.random.randint(0,100, (10,2)) # 10 random points
poly = shapely.geometry.MultiPoint(points).convex_hull.buffer(1) # a polygon
grid_points = [ shapely.geometry.Point(x,y) for x in range(100) for y in range(100)]
in_poly = np.array([poly.contains(point) for point in grid_points])
#plot
plt.imshow(in_poly.reshape(100,100), origin='lower')
plt.plot(*poly.exterior.xy)
【问题讨论】:
标签: python matplotlib gis polygon shapely