def shp2clip(originfig,ax,shpfile,region):
sf = shp.Reader(shpfile)
vertices = []
codes = []
for shape_rec in sf.shapeRecords():
if shape_rec.record[2] in region: # 对应数字要找到
pts = shape_rec.shape.points
prt = list(shape_rec.shape.parts) + [len(pts)]
for i in range(len(prt) - 1):
for j in range(prt[i], prt[i + 1]):
vertices.append((pts[j][0], pts[j][1]))
codes += [Path.MOVETO]
codes += [Path.LINETO] * (prt[i + 1] - prt[i] - 2)
codes += [Path.CLOSEPOLY]
clip1 = Path(vertices, codes)
clip2 = PathPatch(clip1, transform=ax.transData)
for contour in originfig.collections:
contour.set_clip_path(clip2)
return clip2
这是选择shp文件的代码
import shapefile as shp
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
f

最低0.47元/天 解锁文章
252

被折叠的 条评论
为什么被折叠?



