1.读取nc数据相关信息
import netCDF4
from netCDF4 import Dataset
ncfile = Dataset("/home/hsy/wrf-workstation/wrf-version4.4/WPS/geo_em.d01.nc")
ncfile
print(ncfile.variables.keys())
import matplotlib.pyplot as plt
long = ncfile["XLONG_M"][:]
long
lat = ncfile["XLAT_M"][:]
lat
HGT=ncfile["HGT_M"][:]
HGT.shape
long.shape
lat.shape
plt.contourf(HGT[0])
plt.colorbar(label="HGT_M", orientation="horizontal")
plt.show()
2.利用basemap绘图
#利用basemap绘图
import netCDF4
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
ncfile = Dataset("/home/hsy/wrf-workstation/wrf-version4.4/WPS/geo_em.d01.nc")
ncfile
import matplotlib.pyplot as plt
#开始绘图
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111)
#设定标题
titleName = "geo land use"
#ax.set_title(titleName, pad=6, fontproperties = prop)
# 设置投影方式:cyl为圆柱投影、还可设置merc为mercator投影,lcc兰伯特投影等。
# 参数 "resolution" 用于控制地图面积边缘的精细程度,有 'crude', 'low' and'intermediate' resolution datasets are installed by default.
#如果需要'high' or 'full' resolution dataset,需要`conda install -c conda-forge basemap-data-hires`.
m = Basemap(lat_1=30, lat_2=62, lon_0=103,projection='lcc',resolution='i',
llcrnrlon=80, llcrnrlat=10, urcrnrlon=145, urcrnrlat=55)
# 设置经纬度
# 绘制纬度坐标,纬度每隔10度画一条线,且左侧和右侧标注纬度。
m.drawparallels(np.arange(10., 55., 10.), labels=[1, 1, 0, 0], fontsize=10)
# 绘制经度坐标,经度每隔10度画一条线,且上侧和下侧标注经度。
m.drawmeridians(np.arange(70., 160., 10.), labels=[0, 0, 1, 1], fontsize=10)
#添加中国地图图层
m.readshapefile("/home/hsy/03-datadownload/shpdata/china/china", "China_shp")
#添加大陆地图图层
m.fillcontinents(color = "coral", lake_color="blue")
#设定海洋的颜色
m.drawmapboundary(fill_color="aqua")
#在指定位置绘制坐标点
lon_me = long.mean()
lat_me = lat.mean()
lon_do, lat_do = np.meshgrid(lon_me, lat_me) #生成网格点坐标矩阵
#print(lon,lat)
xi, yi = m(lon_do, lat_do)
#print(xi,yi)
m.plot(xi, yi, "go") #绘制绿色点
#在指定位置标注文字
plt.text(xi, yi, "A1(%5.1fW, %3.1fN)" %(lon_do, lat_do))
3、利用cartopy绘图
利用cartopy绘图的详细内容,可以参考此链接: http://t.csdnimg.cn/GjYUb文章来源:https://www.toymoban.com/news/detail-449803.html
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import cartopy.feature as cfeat
#import reader
#创建画布
proj = ccrs.LambertConformal()
fig = plt.figure(figsize=(15,7))
#创建投影轴
fig, ax = plt.subplots(1,1,subplot_kw={"projection":proj})
lat1=ncfile.variables["XLAT_M"][:]
lon1=ncfile.variables["XLONG_M"][:]
print(lon1[0].shape, lat1[0].shape)
ax.contourf(lon1[0], lat1[0],HGT[0])
filepath = "/home/hsy/03-datadownload/shpdata/china/china.shp"
readershp = shpreader.Reader(filepath)
readershp
geoms = readershp.geometries()
ax.add_geometries(geoms, proj, lw=0.5, fc="none") #facecolor="none"前景色无。
readershp.close()
plt.show()
文章来源地址https://www.toymoban.com/news/detail-449803.html
到了这里,关于保姆级教程:python读取并绘制nc数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!