简体   繁体   English

从PatchCollection添加matplotlib颜色条

[英]Adding a matplotlib colorbar from a PatchCollection

I'm converting a Shapely MultiPolygon to a PatchCollection, and first colouring each Polygon like so: 我正在将一个Shapely MultiPolygon转换为PatchCollection,并首先着色每个Polygon,如下所示:

# 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()

But I'd like to add a colorbar to my plot, based upon the PatchCollection colors. 但是我想根据PatchCollection颜色在我的情节中添加一个颜色条。 I'm not sure how to go about that; 我不知道该如何去做; do I pass the cmap keyword when creating pc ? 在创建pc时传递cmap关键字? How do I then call set_array() with the colours I've used? 然后我如何使用我使用的颜色调用set_array()

I had the same problem a little while ago. 我不久前遇到了同样的问题。 For each polygon I saved the corresponding color to a list named mycolors: 对于每个多边形,我将相应的颜色保存到名为mycolors的列表中:

mycolors=[]
...
mycolors.append(SSTvalue)
path_patch = patches.PathPatch(mypath, lw=1)
mypatches.append(path_patch)

I looped over a series of multipolygons stored in a Shapefile and stored each patch in a collection. 我循环存储在Shapefile中的一系列多边形,并将每个补丁存储在一个集合中。 After that I plotted the polygons using the color information I had stored in the list, which was converted to an array eventually, and added a colorbar: 之后,我使用我在列表中存储的颜色信息绘制了多边形,最终将其转换为数组,并添加了一个颜色条:

p = PatchCollection(mypatches, cmap=plt.get_cmap('RdYlBu_r'), alpha=1.0)
p.set_array(array(mycolors))
p.set_clim([np.ma.min(mycolors),np.ma.max(mycolors)])
plt.colorbar(p,shrink=0.5)

The full script I used to plot temperature values with colors and a colorbar for large marine ecosystems of the world represented by polygons can be found here . 可以在这里找到用于绘制颜色温度值的完整脚本和用多边形表示的世界大型海洋生态系统的颜色条。 Hope this helps. 希望这可以帮助。 Cheers, Trond 干杯,特隆德

There is no need to create an additional list. 无需创建其他列表。 Assuming you work in pandas or numpy arrays. 假设你在pandas或numpy数组中工作。

For example: 例如:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib import cm

fig, ax = plt.subplots()
for c_l ,patches in dict_mapindex_mpl_polygon.items():
    color = df_map_elements.loc[c_l, 'stress_level']
    p = PatchCollection(patches,color=cm.Set2(color),lw=.3,edgecolor='k')
    ax.add_collection(p)
ax.autoscale_view()

p.set(array=df_map_elements['stress_level'].values, cmap='Set2')

fig.colorbar(p, label="Stress (index)")

plt.show()

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

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