赞
踩
python通过rioxarray轻松读取tif数据,并借助cartopy进行可视化,现记录在此分享给更多有需要的同学。
# -*- encoding: utf-8 -*-
'''
@File : GFHD.PY
@Time : 2022/02/28 13:42:59
@Author : HMX
@Version : 1.0
@Contact : kzdhb8023@163.com
'''
# here put the import lib
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cmaps
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
import rioxarray as rxr
def cm2inch(value):
return value/2.54
size1 = 10.5
fontdict = {'weight': 'bold','size':size1,'color':'k','family':'SimHei'}
mpl.rcParams.update(
{
'text.usetex': False,
'font.family': 'stixgeneral',
'mathtext.fontset': 'stix',
"font.family":'serif',
"font.size": size1,
"mathtext.fontset":'stix',
"font.serif": ['Times New Roman'],
}
)
proj=ccrs.PlateCarree()
fig,ax = plt.subplots(1, 1,figsize=(cm2inch(16),cm2inch(9)),dpi=100, subplot_kw={'projection': proj})
extent = [-180,180,-90,90]
ax.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.5, zorder=2,color = 'k')# 添加海岸线
ax.add_feature(cfeature.LAND)#添加陆地
ax.set_xticks(np.arange(extent[0], extent[1] + 1, 60), crs = proj)
ax.set_yticks(np.arange(extent[-2], extent[-1] + 1,30), crs = proj)
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=False))
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=False))
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.minorticks_on()
filename=r'E:\Project\World\GFHD2005\GFHD2005_Resample.TIF'
ds = rxr.open_rasterio(filename)
lon,lat = np.meshgrid(ds['x'],ds['y'])
data = ds[0]
lev=np.arange(0,51,5)
cf=ax.contourf(lon,lat,data,levels=lev,extend='neither',transform=ccrs.PlateCarree(),cmap=cmaps.rainbow)
plt.subplots_adjust(right=0.86)
ax2 = fig.add_axes([0.875,0.17,0.02,0.654])
b=plt.colorbar(cf,shrink=0.93,orientation='vertical',extend='both',pad=0.035,aspect=30,ticks=lev,cax=ax2)
b.ax.set_ylabel(r'冠层高度/$\mathrm{m}$',fontdict = fontdict)
plt.savefig(r'E:\Project\Figure\GFHD2005.png',dpi = 600)
plt.show()
如果对你有帮助的话,请‘点赞’、‘收藏’,‘关注’,你们的支持是我更新的动力。
欢迎关注公众号【森气笔记】。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。