爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 2524|回复: 2

[求助] 求助地图白化问题

[复制链接]

新浪微博达人勋

发表于 2023-4-26 15:50:08 | 显示全部楼层 |阅读模式

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

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

x
Python编程将地图白化后为什么有的地方会有边界框,难道是shp文件的问题么?红色字体为白化部分代码,请大家帮忙看看,下面附上代码:
config = {"font.family":'Times New Roman',"font.size": 12,"mathtext.fontset":'stix'}
rcParams.update(config)
filename=r'D:/abcd/ETOPO2v2c_f4.nc'
proj=ccrs.PlateCarree()
#######设置色块################
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=128):
    new_cmap = mpl.colors.LinearSegmentedColormap.from_list('trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name,a=minval, b=maxval),cmap(np.linspace(minval, maxval, n)))
    return new_cmap
#########设置投影及经纬度范围#####################################################
extent = [119.1, 120.6, 28.5, 29.7]
lonmin, lonmax, latmin, latmax = extent
fig = plt.figure(figsize=(9, 8),dpi=500)
ax = fig.add_axes([0.1, 0.1, 0.8, 0.6], projection=ccrs.PlateCarree())
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.set_xticks(np.arange(lonmin,lonmax+0.6,0.6), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(latmin,latmax+0.4,0.4), crs=ccrs.PlateCarree())
lon_formatter = cticker.LongitudeFormatter()
lat_formatter = cticker.LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
##########读取.NC格式的高程数据##################################################
f=xr.open_dataset(filename)
lon=f['x'][:]
lat=f['y'][:]
height=f['z'][:]
filepath = r'D:/test/_static/Map/Jinhua_country.shp'
shp = shapefile.Reader(filepath)
shp_jh = shapefile.Reader(r'D:/test/_static\Map\zj_city_areas.shp')
cmap_new = truncate_colormap(plt.cm.terrain, 0.23, 0.9) #截取colormap,要绿色以上的(>=0.23)
cmap_new.set_under([198/255,234/255,250/255]) #低于0的填色为海蓝
lev=np.arange(0,2000,200)
norm3 = mpl.colors.BoundaryNorm(lev, cmap_new.N) #标准化level,映射色标
cf=ax.contourf(lon,lat,height,levels=lev,norm=norm3,cmap=cmap_new,extend='neither')
ax.contour(lon,lat,height,levels=np.arange(0,8000,1000),colors='k',linewidths=0.075)
font3={'family':'SimHei','size':12,'color':'k'}
ax.set_title('浙江金华',fontdict=font3,loc='left')
ax.add_feature(cfeature.OCEAN)

for red in shp_jh.shapes():
    city_shp=shp_polygon(red.points)
    if city_shp.contains(Point((120, 29))):
        polygon = Polygon(red.points,fc='none',ec='none')
        ax.add_patch(polygon)
        for contour in cf.collections:
            contour.set_clip_path(polygon)
##########设置colorbar属性######################################################
position=fig.add_axes([0.91, 0.12, 0.025,0.46])
cb=fig.colorbar(cf,cax=position)
ax_1=cb.ax
ax_1.set_title('海拔',fontdict=font3)
ax_1.tick_params(which='major',direction='in',labelsize=10,length=3.5)

微信图片_20230426154924.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2023-4-26 16:30:51 | 显示全部楼层
既画了contourf,又画了contour。但是只对contourf进行了mask。
另外,个人建议把代码的结构捋顺,自己和别人看起来都会很清晰。
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2023-4-27 17:14:36 | 显示全部楼层
edwardli 发表于 2023-4-26 16:30
既画了contourf,又画了contour。但是只对contourf进行了mask。
另外,个人建议把代码的结构捋顺,自己和 ...

谢谢大佬,还在学习中
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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