三分钟学会气象要素六边形分布绘制
本帖最后由 气ython风雨 于 2024-4-30 12:43 编辑前言
气象群内有人问这种图怎么画,我也是第一次见,在网上搜了很久,找到一个方便的库:h3-pandas,下面会基于h3-pandas绘制气象要素的六边形分布地图
01 安装库
<div>pip install h3pandas -i <a href="https://pypi.mirrors.ustc.edu.cn/simple/" target="_blank">https://pypi.mirrors.ustc.edu.cn/simple/</a></div>
02 读取地图数据
import geopandas as gpd
shp = 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 plt
import cartopy.crs as ccrs
from cartopy.io.shapereader import BasicReader
import cartopy.feature as cfeature
provinces = 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()
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 plt
import cartopy.crs as ccrs
from cartopy.io.shapereader import BasicReader
import cartopy.feature as cfeature
provinces = 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”,即可获得数据的下载方式。
prompt: color photo of a hexagonal map, with each region represented by a six-sided shape —c 10 —ar 2:3
文章来源于微信公众号:气ython风雨
页:
[1]