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[1])
lon=np.array(data[2])
rain=np.array(data[3])
scales = [0.1, 10, 25, 50, 75, 100]
cmap = cmaps.rainbow
boundaries = [0, 0.1, 10, 25, 50, 75, 100, 150]
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([97, 109, 26, 34], crs=projection)
shap=Reader('SCmap.shp').geometries()
sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none')
ax.add_feature(sichuan)
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[0], lon, np.nan)
masked_lat = np.where(rain < scales[0], lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[0],color=marker_colors[0],zorder=1)
for x in range(1, len(scales)):
masked_lon = np.where(rain >= scales[x - 1], lon, np.nan)
masked_lon = np.where(rain < scales[x], masked_lon, np.nan)
masked_lat = np.where(rain >= scales[x - 1], lat, np.nan)
masked_lat = np.where(rain < scales[x], masked_lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[x],color=marker_colors[x],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[1])
lon=np.array(data[2])
rain=np.array(data[3])
# 设置colorbar刻度及区间色调
scales = [0.1, 10, 25, 50, 75, 100]
cmap = cmaps.rainbow
boundaries = [0, 0.1, 10, 25, 50, 75, 100, 150]
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([97, 109, 26, 34], 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[0], lon, np.nan)
masked_lat = np.where(rain < scales[0], lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[0],color=marker_colors[0],zorder=1)
for x in range(1, len(scales)):
masked_lon = np.where(rain >= scales[x - 1], lon, np.nan)
masked_lon = np.where(rain < scales[x], masked_lon, np.nan)
masked_lat = np.where(rain >= scales[x - 1], lat, np.nan)
masked_lat = np.where(rain < scales[x], masked_lat, np.nan)
plt.scatter(masked_lon,masked_lat,s=sizes[x],color=marker_colors[x],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')
后台回复:第八星系
即可入群,
群内更新每日推文所需数据
进群请勿回复 第八星系 以外的字词
本文编辑:myp