- 积分
- 8807
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-5-23
- 最后登录
- 1970-1-1
|
发表于 2018-10-10 21:01:56
|
显示全部楼层
本帖最后由 eeeee 于 2018-10-11 18:31 编辑
1.用shapefile模块把你想要的区域的点提取出来,很多shp里面都带有地区名称,可以根据那个来筛选
2.把提取出来的点转换成matplotlib的path
3.用path来白化
附一段我之前用过的shapefile转pathpatch的代码
- from ..constants import modpath
- from ..error import RadarPlotError
- import os
- import numpy as np
- import shapefile
- from matplotlib.path import Path
- from matplotlib.patches import PathPatch
- def highlight_area(area):
- r'''Return pathpatch for given area name'''
- fpath = os.path.join(modpath, 'shapefile', 'City')
- shp = shapefile.Reader(fpath)
- rec = shp.shapeRecords()
- vertices = list()
- codes = list()
- for i in area:
- if not isinstance(i, str):
- raise RadarPlotError('Area name should be str')
- name = np.array([i.record[2].decode('GBK') for i in rec])
- mask = np.ma.array(name, mask=(name==i))
- target = np.array(rec)[mask.mask]
- for j in target:
- codes += [Path.MOVETO] + [Path.LINETO] * (len(j.shape.points) - 1)
- vertices += j.shape.points
- codes += [Path.CLOSEPOLY]
- vertices += [j.shape.points[0]]
- path = Path(vertices, codes)
- patch = PathPatch(path, facecolor='None', edgecolor='red')
- return patch
复制代码 |
|