|
导入库并读取数据 import numpy as npimport pandas as pdimport cartopy.crs as ccrsimport cartopy.feature as cfeatureimport matplotlib.pyplot as pltfrom matplotlib import colors, cmimport cmapsimport geocat.viz as gvfrom cartopy.io.shapereader import Readerdata=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])
; K+ o$ z( E; S* i设置colorbar刻度及区间色调 scales = [0.1, 10, 25, 50, 75, 100]cmap = cmaps.rainbowboundaries = [0, 0.1, 10, 25, 50, 75, 100, 150]norm = colors.BoundaryNorm(boundaries, cmap.N)mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
( K, |$ l' }3 r4 i设置散点标记的颜色区间 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)
4 I, v( k' u7 Y3 i9 o添加四川地图 shap=Reader('SCmap.shp').geometries()sichuan = cfeature.ShapelyFeature(shap,crs=ccrs.PlateCarree(),edgecolor='k', facecolor='none')ax.add_feature(sichuan)
; {, X; k' X) M7 @- I3 ]/ o8 Z设置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 plotax.tick_params(axis='both', which='both', top=False, right=False)
/ b4 }8 m: f% L0 f( {/ G8 w3 a, e! L% o' n' D6 |- K# ^
绘制不同降水区间散点图 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)7 ?( E$ N" O3 `) n* \8 ?- K
考标记出某一站点 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') D# _( r, G9 B4 O
3 F. j# n$ U5 m- r" z, }% |
|