第八星系人气爱 发表于 2024-3-1 10:40:26

python基于站点经纬度绘制降水空间散点分布图


作者:第八星系-刘术辉
邮箱:1211284952@qq.com


导入库并读取数据
import numpy as np
import pandas as pd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import colors, cm
import cmaps
import geocat.viz as gv
from cartopy.io.shapereader import Reader

data=pd.read_csv('20190722.csv',dtype=np.float64,header=None,delimiter=',',encoding='gbk')
lat=np.array(data)
lon=np.array(data)
rain=np.array(data)

设置colorbar刻度及颜色区间
scales =
cmap = cmaps.rainbow

boundaries =
norm = colors.BoundaryNorm(boundaries, cmap.N)
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)

设置散点标记的颜色区间
marker_colors = mappable.to_rgba(boundaries)

sizes = np.geomspace(10, 250, len(boundaries))

plt.figure(figsize=(9, 6))
projection = ccrs.PlateCarree()
ax = plt.axes(projection=projection)
ax.set_extent(, crs=projection)

添加四川地图
shap=Reader('SCmap.shp').geometries()
sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none')
ax.add_feature(sichuan)

设置x,y轴经纬度刻度
gv.set_axes_limits_and_ticks(ax,xticks=np.linspace(97, 109, 5),yticks=np.linspace(26, 34, 5))
gv.add_lat_lon_ticklabels(ax)
gv.add_major_minor_ticks(ax,x_minor_per_major=1,y_minor_per_major=1,labelsize=12)

# Remove ticks on the top and right sides of the plot
ax.tick_params(axis='both', which='both', top=False, right=False)

绘制不同区域降水散点图
masked_lon = np.where(rain < scales, lon, np.nan)
masked_lat = np.where(rain < scales, lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes,color=marker_colors,zorder=1)

for x in range(1, len(scales)):
    masked_lon = np.where(rain >= scales, lon, np.nan)
    masked_lon = np.where(rain < scales, masked_lon, np.nan)
    masked_lat = np.where(rain >= scales, lat, np.nan)
    masked_lat = np.where(rain < scales, masked_lat, np.nan)
    plt.scatter(masked_lon,masked_lat,s=sizes,color=marker_colors,zorder=1)

masked_lon = np.where(rain >= scales[-1], lon, np.nan)
masked_lat = np.where(rain >= scales[-1], lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[-1],color=marker_colors[-1],zorder=1)

标记出某一站点
plt.colorbar(mappable=mappable,ax=ax,orientation='horizontal',label='Rainfall Amount(mm)',
             drawedges=True,format='%.2f',ticks=scales)
plt.scatter(103.12,30.08,s=20)
plt.annotate(r'$mingshan$', xy=(103.12,30.08),xytext=(4,-100),xycoords='data',textcoords='offset points',
             fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3'))

plt.savefig('test.png')

完整代码
import numpy as np
import pandas as pd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import colors, cm
import cmaps
import geocat.viz as gv
from cartopy.io.shapereader import Reader

data=pd.read_csv('20190722.csv',dtype=np.float64,header=None,delimiter=',',encoding='gbk')
lat=np.array(data)
lon=np.array(data)
rain=np.array(data)

# 设置colorbar刻度及区间色调
scales =
cmap = cmaps.rainbow

boundaries =
norm = colors.BoundaryNorm(boundaries, cmap.N)
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)

# 设置散点标记的颜色区间
marker_colors = mappable.to_rgba(boundaries)

sizes = np.geomspace(10, 250, len(boundaries))

plt.figure(figsize=(9, 6))
projection = ccrs.PlateCarree()
ax = plt.axes(projection=projection)
ax.set_extent(, crs=projection)

# 添加四川地图
shap=Reader('SCmap.shp').geometries()
sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none')
ax.add_feature(sichuan)

# 设置x、y轴经纬度刻度
gv.set_axes_limits_and_ticks(ax,xticks=np.linspace(97, 109, 5),yticks=np.linspace(26, 34, 5))
gv.add_lat_lon_ticklabels(ax)
gv.add_major_minor_ticks(ax,x_minor_per_major=1,y_minor_per_major=1,labelsize=12)

# Remove ticks on the top and right sides of the plot
ax.tick_params(axis='both', which='both', top=False, right=False)

# 绘制不同降水区间散点图
masked_lon = np.where(rain < scales, lon, np.nan)
masked_lat = np.where(rain < scales, lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes,color=marker_colors,zorder=1)

for x in range(1, len(scales)):
    masked_lon = np.where(rain >= scales, lon, np.nan)
    masked_lon = np.where(rain < scales, masked_lon, np.nan)
    masked_lat = np.where(rain >= scales, lat, np.nan)
    masked_lat = np.where(rain < scales, masked_lat, np.nan)
    plt.scatter(masked_lon,masked_lat,s=sizes,color=marker_colors,zorder=1)

masked_lon = np.where(rain >= scales[-1], lon, np.nan)
masked_lat = np.where(rain >= scales[-1], lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[-1],color=marker_colors[-1],zorder=1)

# 标记出某一站点
plt.colorbar(mappable=mappable,ax=ax,orientation='horizontal',label='Rainfall Amount(mm)',
             drawedges=True,format='%.2f',ticks=scales)
plt.scatter(103.12,30.08,s=20)
plt.annotate(r'$mingshan$', xy=(103.12,30.08),xytext=(4,-100),xycoords='data',textcoords='offset points',
             fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3'))

plt.savefig('test.png')

微信搜索“第八星系人造大气理论爱好者”公众号,关注获取文章数据

页: [1]
查看完整版本: python基于站点经纬度绘制降水空间散点分布图