- 积分
- 1385
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-12-16
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 jack_ln 于 2023-3-17 14:26 编辑
1、Python绘图:使用Basemap绘制全球地形图_菩提有树的博客-CSDN博客
2、Python绘制中南半岛与中国南海DEM地形图 (qq.com)
参考了文章1中的地形配色,综合以上链接,代码如下。
- # -*- encoding: utf-8 -*-
- import numpy as np
- import matplotlib.pyplot as plt
- import cartopy.crs as ccrs
- import cartopy.feature as cfeature
- import cartopy.mpl.ticker as cticker
- import xarray as xr
- from matplotlib import rcParams
- from cartopy.io.shapereader import Reader
- config = {"font.family":'Times New Roman',"font.size": 12,"mathtext.fontset":'stix'}
- rcParams.update(config)
- filename=r'ETOPO2v2c_f4.nc'
- proj=ccrs.PlateCarree()
- #########设置投影及经纬度范围#####################################################
- extent = [70, 140, 10, 60]
- lonmin, lonmax, latmin, latmax = extent
- fig = plt.figure(figsize=(9, 8),dpi=300)
- 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+10,10), crs=ccrs.PlateCarree())
- ax.set_yticks(np.arange(latmin,latmax+5,5), 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'][:]
- levels = [-8000, -6000, -4000, -2000, -1000, -200, -50, 0, 50, 200, 500, 1000, 1500, 2000, 3000, 4000,
- 5000, 6000, 7000, 8000] # 创建分级
- color = ['#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#006837',
- '#31a354', '#78c679', '#addd8e', '#d9f0a3', '#f7fcb9', '#c9bc87', '#a69165', '#856b49',
- '#664830', '#ad9591', '#d7ccca'] # 设置色带
- cf=ax.contourf(lon,lat,height,levels=levels,colors=color,extend='neither')
- font3={'family':'SimHei','size':12,'color':'k'}
- ax.add_feature(cfeature.OCEAN)
- ##########设置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)
- ################加载shp文件######################################################
- ax.add_feature(cfeature.LAKES.with_scale('50m'))
- ax.add_geometries(Reader(r'country1.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=1)
- ax.add_geometries(Reader(r'province.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=1)
- plt.savefig('china.png',dpi=300,bbox_inches='tight',pad_inches=0)
复制代码
|
|