三分钟学会气象要素六边形分布绘制

文摘   2024-07-24 14:39   广东  



前言

气象群内有人问这种图怎么画,我也是第一次见,在网上搜了很久,找到一个方便的库:h3-pandas,下面会基于h3-pandas绘制气象要素的六边形分布地图



01
安装库

pip install h3pandas -i https://pypi.mirrors.ustc.edu.cn/simple/

02
读取地图数据

import geopandas as gpdshp = gpd.read_file("/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp")shp.plot()


03
地图数据重采样为六边形

import h3pandas
# 设置分辨率
resolution = 3
# 将地图数据重采样为六边形格子
hexagons = shp.h3.polyfill_resample(resolution)
hexagons.plot()


04
简单绘图

import matplotlib.pyplot as pltimport cartopy.crs as ccrsfrom cartopy.io.shapereader import BasicReaderimport cartopy.feature as cfeatureprovinces = BasicReader("/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp")fig = plt.figure(figsize=(15, 12),dpi=200)ax = fig.add_subplot(111,projection=ccrs.PlateCarree())ax.add_feature(cfeature.LAND)ax.add_feature(cfeature.OCEAN)ax.add_feature(cfeature.COASTLINE)ax.add_geometries(provinces.geometries(), linewidth=.5, edgecolor='black', crs=ccrs.PlateCarree(),                  facecolor='none')# 绘制六边形地图hexagons.plot(ax=ax, color='r',alpha=0.3,edgecolor='black', linewidth=0.5) # 设置颜色和透明度
# 显示图形plt.show()

05
气象数据读取

import meteva.base as meb
filename = "/home/mw/input/meteva2260/22052019.000" # 替换为你的micaps文件路径
sta = meb.read_stadata_from_micaps3(filename)
sta.fillna(0, inplace=True)
sta.head()


leveltimedtimeidlonlatdata0
002022-05-20 19:00:00050246124.7252.350.1
102022-05-20 19:00:00050645124.4848.480.1
202022-05-20 19:00:00050646124.8848.501.0
302022-05-20 19:00:00050656126.5148.261.0
402022-05-20 19:00:00050659126.2448.050.4

06
数据处理:将气象站点数据转格式六边形表格拼接

# 将站点降水数据转换为geopandas的DataFrame对象geometry = gpd.points_from_xy(sta.lon, sta.lat)precipitations = gpd.GeoDataFrame(sta, geometry=geometry)
# 将六边形地图表格与站点降水数据进行空间连接merged_data = gpd.sjoin(hexagons, precipitations, op='contains')
# 计算每个六边形的平均降水量merged_data['mean_precipitation'] = merged_data.groupby('index')['data0'].mean().reset_index()['data0']
表格列数过多,显示效果不佳,总之是计算后的平均降水量加入了merged_data

07
数据可视化

import matplotlib.pyplot as pltimport cartopy.crs as ccrsfrom cartopy.io.shapereader import BasicReaderimport cartopy.feature as cfeatureprovinces = BasicReader("/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp")fig = plt.figure(figsize=(15, 12),dpi=200)ax = fig.add_subplot(111,projection=ccrs.PlateCarree())ax.add_feature(cfeature.LAND)ax.add_feature(cfeature.OCEAN)ax.add_feature(cfeature.COASTLINE)ax.add_geometries(provinces.geometries(), linewidth=.5, edgecolor='black', crs=ccrs.PlateCarree(),                  facecolor='none')merged_data.plot(column='mean_precipitation',ax=ax,cmap='viridis',color='r', legend=True,edgecolor='b', linewidth=0.5)
<GeoAxes: >

可视化效果还行吧,h3pandas库还是很强的
geopandas的可视化模块我还没摸透,上面的plot语句必须要color参数才能出图,不知道是不是版本问题,了解这个的大佬欢迎讨论。
想要复现的小伙伴可以后台私信“h3pandas”,即可获得数据的下载方式。


封面由ai绘制

prompt: color photo of a hexagonal map, with each region represented by a six-sided shape —c 10 —ar 2:3






第八星系人造大气理论爱好者
记录与交流python、matlab等科研工具。记录与交流大气科学的学科知识
 最新文章