我正在将 Shapely MultiPolygon 转换为 PatchCollection,并首先像这样为每个多边形着色:
# ldn_mp is a MultiPolygon
cm = plt.get_cmap('RdBu')
num_colours = len(ldn_mp)
fig = plt.figure()
ax = fig.add_subplot(111)
minx, miny, maxx, maxy = ldn_mp.bounds
w, h = maxx - minx, maxy - miny
ax.set_xlim(minx - 0.2 * w, maxx + 0.2 * w)
ax.set_ylim(miny - 0.2 * h, maxy + 0.2 * h)
ax.set_aspect(1)
patches = []
for poly in ldn_mp:
colour = cm(1. * len(filter(poly.contains, points)) / num_colours)
patches.append(PolygonPatch(poly, fc=colour, ec='#555555', lw=0.2, alpha=1., zorder=1))
pc = PatchCollection(patches, match_original=True)
ax.add_collection(pc)
ax.set_xticks([])
ax.set_yticks([])
plt.title("Density of NO$^2$ Sensors by Borough")
plt.tight_layout()
plt.show()
但我想根据 PatchCollection 颜色在我的绘图中添加一个颜色条。我不知道该怎么做。创建时要传递cmap
关键字pc
吗?然后我如何set_array()
用我用过的颜色打电话?