python - 多个形状内的 basemap 轮廓

标签 python shapefile matplotlib-basemap contourf

我试图在 map 上仅在形状文件中包含的城镇范围内绘制插值天气数据。以下是使用导入的 shapefile 在 basemap 上未裁剪的轮廓: Contourf overlaid on Basemap with Shapefile

我尝试通过迭代轮廓集合来剪切轮廓集合,如下所示:

m.readshapefile('data/grense', 'grense',zorder=10,linewidth=1, 
drawbounds=True)

patches   = []
for info, shape in zip(m.grense_info, m.grense):
   patches.append( Polygon(np.array(shape), linestyle=':', fill=False) )

for poly in patches:
   for collection in cs.collections:
      collection.set_clip_path(poly)

这显然将轮廓限制在一个多边形,即一个城镇,如下所示: Contourf clipped to one ploygon

是否可以创建轮廓集合的集合,然后使用 ax.add_collection(new_contour_collection) 添加该集合?大致如下:

for poly in patches:
   for collection in cs.collections:
     contour_collection.append(collection)
ax.add_collection(contour_collection)

或者我可以从 Patchcollection 创建单个路径,然后使用 collection.set_clip_patch(patches) 剪辑每个轮廓集合吗?

最佳答案

已关注 swatchai的建议和Thomas Kühn之前的回答here ,我设法解决了我的问题,见这里 masked interpolation ,

通过执行以下操作:

#Combine shapefile shape object (m.grense) with map edges
##limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0],[x1,y0],[x1,y1],[x0,y1]])

polys = [map_edges] + m.grense

codes = [
    [Path.MOVETO] + [Path.LINETO for p in p[1:]]
    for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)

#Important - Set Zorder greater than Contour and less than Map 
borders
patch = PathPatch(path,facecolor='white', lw=0, zorder =2)

##masking the data:
ax.add_patch(patch)

关于python - 多个形状内的 basemap 轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699083/

相关文章:

python - 如何修复 pygame 中的 Sprite 动画?

python - 如何防止每次运行测试用例时移动应用程序关闭并重新打开?

python - 绘制由散点数据决定颜色的形状文件

java - 在 Geotools 中构建 Shapefile 时遇到问题

python - 在读取 shapefile 时从 dbf 文件中删除空值以解决 Matplotlib Basemap 中的错误

python - 如何在 seaborn 情节中重新标记条形图?

python - SeLU 激活函数 x 参数导致类型错误

r - 如何从人口普查形状文件(邮政编码级别)中删除所有小岛?

anaconda - 如何将anaconda的python版本从3.4更改为3.3

python - 在 basemap 上绘图 - 意外结果