- 积分
- 208
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-3-24
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
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)
|
-
|