【python地理信息绘制入门】cartopy学习—contourf

u500图像绘制

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

import matplotlib.pyplot as plt
import xarray as xr
import warnings
warnings.filterwarnings("ignore")
import numpy as np

plt.rcParams['font.sans-serif']=['Arial']
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['axes.labelsize']=15
plt.rcParams['xtick.labelsize']=15
plt.rcParams['ytick.labelsize']=15
lons = np.linspace(-180, 179.75, 1440)
lats = np.linspace(90, -90, 721)
lons, lats = np.meshgrid(lons, lats)
data = xr.open_dataset("D:\\tmp\\download\\z500\\u500_2023.nc")
u = data["u"][0,0,:,:]

# 创建一个图形和轴
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# 绘制等高线填充图
contourf = ax.contourf(lons, lats, u, levels=20, cmap='viridis')

# 添加海岸线和国界线特征
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.set_global()  # 设置投影为全球
#x,y坐标轴设置
ax.set_xticks(np.arange(-180, 180+10, 20))
ax.set_yticks(np.arange(-90, 90+10, 20))

# 使用 inset_axes 创建一个与图像等高的轴,放置颜色条
axins = inset_axes(ax, width="5%", height="100%", loc='lower right', bbox_to_anchor=(0.1, -0.01, 1, 1), bbox_transform=ax.transAxes)
cbar = plt.colorbar(contourf, cax=axins)
cbar.set_label('Data Values')

# 设置标题
ax.set_title('Global Contourf Plot with $u500$', fontsize=18)

plt.show()

【python地理信息绘制入门】cartopy学习—contourf_第1张图片

你可能感兴趣的:(数据可视化,python,学习,开发语言)