import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import numpy as np
import shapefile
from mpl_toolkits.basemap import Basemap
import xarray as xr
ds = xr.open_dataset(r"D:\论文\论文数据\DEM\cldasgrid_dem.nc")
plt.rcParams.update({'font.size': 20})
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure(figsize=[8, 12])
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
sf = shapefile.Reader(r"D:\论文\论文数据\Shp\ditu\新疆维吾尔自治区\Xinjiang_province.shp")
shapes = sf.shapes()
codes = []
pts = shapes[0].points
prt = list(shapes[0].parts) + [len(pts)]
for i in range(len(prt) - 1):
codes += [Path.MOVETO]
codes += [Path.LINETO] * (prt[i+1] - prt[i] -2)
codes += [Path.CLOSEPOLY]
clip = Path(pts, codes)
clip = PathPatch(clip, transform=ax1.transData)
m = Basemap(llcrnrlon=75,
llcrnrlat=35,
urcrnrlon=95,
urcrnrlat =50,
resolution=None,
projection='cyl')
m.readshapefile(r"D:\论文\论文数据\Shp\ditu\新疆维吾尔自治区\Xinjiang_city", 'Xinjiang_city Map', color='k', linewidth=1.2)
m.readshapefile(r"D:\论文\论文数据\Shp\ditu\新疆维吾尔自治区\Xinjiang_province", 'Xinjiang Map', color='k', linewidth=1.2)
parallels = np.arange(35, 51, 5)
m.drawparallels(parallels, labels=[True, False, True, True], color='dimgrey', dashes=[1, 3], fontsize=12)
meridians = np.arange(75, 96, 5)
m.drawmeridians(meridians, labels=[True, True, False, True], color='dimgrey', dashes=[1, 3], fontsize=12)
lon = np.linspace(min(ds['LON'].data), max(ds['LON'].data), len(ds['LON'].data)) # 经度
lat = np.linspace(min(ds['LAT'].data), max(ds['LAT'].data), len(ds['LAT'].data)) # 纬度
lon, lat = np.meshgrid(lon, lat)
dem = ds['elevation'].data # 读入dem数据变量即可
print(dem)
levels = np.arange(300, 8200, 10)
cbar_kwargs = {
'orientation': 'vertical',
'label': 'DEM',
'shrink': 0.02,
'ticks': np.arange(300, 8200+2, 1000),
'pad': -0.1,
'shrink': 0.95
}
m.contourf(lon, lat, dem, levels=levels, cbar_kwargs=cbar_kwargs, extend='both', cmap='RdYlGn')
cb = m.colorbar(location='right', pad=0.1)
cb.ax.tick_params(labelsize=8)
cb.set_label("高度(m)", fontsize=12)
plt.ylabel('')
plt.xlabel('')
plt.rcParams.update({'font.size': 30})
ax1.set_title(u' ', color='black', fontsize=15)
lon = 82.43
tip0 = 37.04
plt.text(lon-0.45, lat+0.05, u"民丰站", fontsize=13)
plt.scatter(lon, lat, marker='.', s=30, color="green")
plt.show()
画出来的图片如下:
SHP和DEM数据联系即可发送(私信)