python讀取并繪制nc數(shù)據(jù)的保姆級(jí)教程
讀取nc數(shù)據(jù)相關(guān)信息
#導(dǎo)入庫(kù)
import netCDF4
from netCDF4 import Dataset
#讀取數(shù)據(jù)文件
nc_file=Dataset("/media/hsy/HSYWS/001DATA/VPD_DATA/vapor_pressure_deficit_1979.nc")
#輸出數(shù)據(jù)文件的兩種方式
#nc_file
print(nc_file)輸出結(jié)果展示:
<class 'netCDF4._netCDF4.Dataset'> root group (NETCDF4 data model, file format HDF5): Conventions: CF-1.4
created_by: R, packages ncdf4 and raster (version 3.3-13)
date: 2021-10-08 13:21:50
dimensions(sizes): Longitude(1440), Latitude(721), Time(365)
variables(dimensions): int32 crs(), float64 Longitude(Longitude), float64 Latitude(Latitude), int32 Time(Time), float32 VPD(Time, Latitude, Longitude)
groups:
#所有變量讀取
print(nc_file.variables.keys())
#輸出結(jié)果:dict_keys(['crs', 'Longitude', 'Latitude', 'Time', 'VPD'])
#單個(gè)變量讀取
nc_file['Longitude']
#print(nc_file.variables['Longitude'])
"""<class 'netCDF4._netCDF4.Variable'>
float64 Longitude(Longitude)
units: degrees_east
long_name: Longitude
unlimited dimensions:
current shape = (1440,)
filling on, default _FillValue of 9.969209968386869e+36 used"""
print(nc_file.variables['crs'])結(jié)果輸出解讀:
<class 'netCDF4._netCDF4.Variable'> #文件數(shù)據(jù)類型
int32 crs()
proj4: +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs #proj4坐標(biāo)系參數(shù),詳情請(qǐng)見(jiàn):Quick start — PROJ 9.1.0 documentation
unlimited dimensions:
current shape = () filling on, default _FillValue of -2147483647 used
#單個(gè)變量的所有屬性名稱 print(nc_file.variables['VPD'].ncattrs()) #['_FillValue', 'long_name', 'grid_mapping', 'proj4', 'min', 'max'] print(nc_file.variables['VPD'].proj4)#proj4坐標(biāo)系 print(nc_file.variables['VPD'].grid_mapping)#給定坐標(biāo)變量與真實(shí)經(jīng)緯度坐標(biāo)之間的映射關(guān)系:crs print(nc_file.variables['VPD']._FillValue)#填充值或空值 #讀取變量的維度 print(nc_file['VPD'].shape) #(365, 721, 1440) (#time, latitude, longitude) 格點(diǎn)分辨率為:天*0.25*0.25度。 #讀取變量值 VPD=nc_file.variables['VPD'][:] print(VPD)#讀取結(jié)果含有全部數(shù)值。 #print(nc_file['VPD'])#輸出結(jié)果不完整
繪圖
1、利用matplotlib繪圖
import matplotlib.pyplot as plt plt.contourf(long, lat, VPD[10, :, :] ) plt.colorbar(label="VPD", orientation="horizontal") plt.show()

2、利用basemap繪圖
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
lon0 = long.mean()
lat0 = lat.mean()
# 設(shè)置投影方式:cyl為圓柱投影、還可設(shè)置merc為mercator投影 llcrnrlat為起始lat;urcrnrlat為終止lat
# m = Basemap(projection='merc', llcrnrlat=lat[0], urcrnrlat=lat[-1], \
# llcrnrlon=lon[0], urcrnrlon=lon[-1], ax=ax1)
# 參數(shù) "resolution" 用于控制地圖面積邊緣的精細(xì)程度,有'l'和'h'兩種取值
m = Basemap(lat_0=lat0, lon_0=lon0,projection='cyl',resolution='l')
# 繪制等經(jīng)緯度線 緯度每隔20度畫(huà)一條線,且標(biāo)注經(jīng)緯度
m.drawparallels(np.arange(-90., 91., 20.), labels=[1, 0, 0, 0], fontsize=10)
m.drawmeridians(np.arange(-180., 181., 40.), labels=[0, 0, 0, 1], fontsize=10)
m.drawcoastlines()# 繪制海岸線
lon, lat = np.meshgrid(long, lat)
xi, yi = m(lon, lat)
# cmap是圖形顏色,還可選‘jet'、‘spring'、‘winter'、'summer'、'autumn'
cs = m.contourf(xi, yi, VPD[10], cmap='summer')
# pad指位置,
cbar = m.colorbar(cs, location='bottom', pad="10%",format='%.1f')
font1 = {'family': 'DejaVu Sans', 'weight': 'normal', 'size': 16}
plt.title('VPD', font1)
plt.show()3、利用cartopy繪圖
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
proj = ccrs.PlateCarree()
fig = plt.figure(figsize=(15, 7))
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': proj})
# 或者 ax = fig.add_subplot(111,proj=proj)
lon1 = nc_file.variables['Longitude'][:]
lat1 = nc_file.variables['Latitude'][:]
print(lon1.shape, lat1.shape)
ax.contourf(lon, lat, VPD[100])
ax.coastlines(resolution = '10m')
#添加格網(wǎng)
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
# 設(shè)置 gridlines 和 ticklabels
gl = ax.gridlines(draw_labels = True, linewidth = 1.5)
gl.xlabels_top = False
gl.xlines = True
gl.xformatter = LONGITUDE_FORMATTER
gl.ylabels_right = False
gl.ylines = True
gl.yformatter = LATITUDE_FORMATTER
plt.show()
總結(jié)
到此這篇關(guān)于python讀取并繪制nc數(shù)據(jù)的保姆級(jí)教程的文章就介紹到這了,更多相關(guān)python讀取繪制nc數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jupyter notebook遠(yuǎn)程訪問(wèn)服務(wù)器的方法
今天小編就為大家分享一篇Jupyter notebook遠(yuǎn)程訪問(wèn)服務(wù)器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python實(shí)現(xiàn)生成Word、docx文件的方法分析
這篇文章主要介紹了python實(shí)現(xiàn)生成Word、docx文件的方法,結(jié)合實(shí)例形式分析了Python使用docx模塊操作word文件與docx文件的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
python基礎(chǔ)教程之匿名函數(shù)lambda
這篇文章主要介紹了 python基礎(chǔ)教程之匿名函數(shù)lambda的相關(guān)資料,需要的朋友可以參考下2017-01-01
Python3實(shí)現(xiàn)的簡(jiǎn)單驗(yàn)證碼識(shí)別功能示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的簡(jiǎn)單驗(yàn)證碼識(shí)別功能,涉及Python針對(duì)驗(yàn)證碼圖片識(shí)別處理相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python求均值,方差,標(biāo)準(zhǔn)差的實(shí)例
今天小編就為大家分享一篇Python求均值,方差,標(biāo)準(zhǔn)差的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python編寫(xiě)簡(jiǎn)單的HTML頁(yè)面合并腳本
這篇文章主要介紹了Python編寫(xiě)簡(jiǎn)單的HTML頁(yè)面合并腳本的相關(guān)資料,需要的朋友可以參考下2016-07-07
Python中sorted()函數(shù)之排序的利器詳解
sorted()函數(shù)是Python中的內(nèi)置函數(shù),用于對(duì)可迭代對(duì)象進(jìn)行排序,下面這篇文章主要給大家介紹了關(guān)于Python中sorted()函數(shù)之排序的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08

