请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 12876|回复: 5

[求助] Python抠底图+填色白化有空白

[复制链接]

新浪微博达人勋

发表于 2018-10-10 14:56:26 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
各位大神:
      本人刚接触Python,有不懂的地方请教一下。如何将某省的几个城市底图拿出来,然后在这个区域内填色显示变量,同时进行白化,效果图没有空白锯齿。

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-10 21:01:56 | 显示全部楼层
本帖最后由 eeeee 于 2018-10-11 18:31 编辑

1.用shapefile模块把你想要的区域的点提取出来,很多shp里面都带有地区名称,可以根据那个来筛选
2.把提取出来的点转换成matplotlib的path
3.用path来白化
附一段我之前用过的shapefile转pathpatch的代码
  1. from ..constants import modpath
  2. from ..error import RadarPlotError
  3. import os
  4. import numpy as np
  5. import shapefile
  6. from matplotlib.path import Path
  7. from matplotlib.patches import PathPatch

  8. def highlight_area(area):
  9.     r'''Return pathpatch for given area name'''
  10.     fpath = os.path.join(modpath, 'shapefile', 'City')
  11.     shp = shapefile.Reader(fpath)
  12.     rec = shp.shapeRecords()
  13.     vertices = list()
  14.     codes = list()
  15.     for i in area:
  16.         if not isinstance(i, str):
  17.             raise RadarPlotError('Area name should be str')
  18.         name = np.array([i.record[2].decode('GBK') for i in rec])
  19.         mask = np.ma.array(name, mask=(name==i))
  20.         target = np.array(rec)[mask.mask]
  21.         for j in target:
  22.             codes += [Path.MOVETO] + [Path.LINETO] * (len(j.shape.points) - 1)
  23.             vertices += j.shape.points
  24.         codes += [Path.CLOSEPOLY]
  25.         vertices += [j.shape.points[0]]
  26.         path = Path(vertices, codes)
  27.     patch = PathPatch(path, facecolor='None', edgecolor='red')
  28.     return patch
复制代码
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-10-11 11:56:41 | 显示全部楼层
eeeee 发表于 2018-10-10 21:01
1.用shapefile模块把你想要的区域的点提取出来,很多shp里面都带有地区名称,可以根据那个来筛选
2.把提取 ...

谢谢大神,好好学习一下
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-10-11 11:56:45 | 显示全部楼层
eeeee 发表于 2018-10-10 21:01
1.用shapefile模块把你想要的区域的点提取出来,很多shp里面都带有地区名称,可以根据那个来筛选
2.把提取 ...

谢谢大神,好好学习一下
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-12 13:15:51 | 显示全部楼层
eeeee 发表于 2018-10-10 21:01
1.用shapefile模块把你想要的区域的点提取出来,很多shp里面都带有地区名称,可以根据那个来筛选
2.把提取 ...

请教下如何用path做白化
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-10-12 14:08:54 | 显示全部楼层
本帖最后由 eeeee 于 2018-10-12 14:11 编辑
chongzika 发表于 2018-10-12 13:15
请教下如何用path做白化

https://matplotlib.org/api/_as_g ... .set_clip_path.html

  1. for collection in artist.collections:
  2.     collection.set_clip_path(patch)
复制代码


这里的artist就是调用contour,contourf等绘图指令之后返回的对象
PS:之前那里打错了,是用pathpatch来做白化

密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表