国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python白化南海小地圖(文末附數(shù)據(jù)和完整代碼)

本文作者:謝金材,南京信息工程大學(xué)碩士

聯(lián)系郵箱:jincaixie@163.com


使用氣科院馬嘉理博士編寫的白化函數(shù)maskout.py白化南海小地圖時會出現(xiàn)南海小地圖區(qū)域外數(shù)據(jù)無法白化的現(xiàn)象。
繪圖結(jié)果如下:

通過改進,在shp2clip加入了一個判斷,將所有南海外的經(jīng)緯度值等于南海小地圖左上角的經(jīng)緯度值,成功實現(xiàn)南海小地圖區(qū)域外數(shù)據(jù)白化。
繪圖結(jié)果如下:


完整代碼如下:

# -*- coding: utf-8 -*-'''Created on Sat Apr 15 16:02:47 2023@author: 31015'''import numpy as npimport xarray as xrimport matplotlib.pyplot as pltimport scipyimport cartopy.crs as ccrsimport cartopy.mpl.ticker as ctickerimport matplotlib.ticker as mtickerimport cartopy.io.shapereader as shpreaderimport maskoutfilename=r'H:/NCEP/monthly/hgt.mon.mean.nc'ds=xr.open_dataset(filename)z3=np.array(ds['hgt'].loc[ds.time.dt.month.isin([3])].loc['1979-01-01':'2022-12-31',500,:,:],dtype='float32').squeeze()z4=np.array(ds['hgt'].loc[ds.time.dt.month.isin([4])].loc['1979-01-01':'2022-12-31',500,:,:],dtype='float32').squeeze()lon=np.array(ds.lon,dtype='float32')lat=np.array(ds.lat,dtype='float32')r=np.empty((73,144))p=np.empty((73,144))for i in range(len(lat)): for j in range((len(lon))):        r[i,j],p[i,j]=scipy.stats.pearsonr(z3[:,i,j],z4[:,i,j])       fig,ax1 = plt.subplots(figsize=(6,5), subplot_kw={'projection':ccrs.PlateCarree()})leftlon, rightlon, lowerlat, upperlat = (70,135,10,60)img_extent = [leftlon, rightlon, lowerlat, upperlat]ax1.set_extent(img_extent, crs=ccrs.PlateCarree())china = shpreader.Reader(r'H:\prediction\map\bou2_4l.dbf').geometries()ax1.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='gray',zorder = 1,lw=0.5)country=shpreader.Reader(r'H:\prediction\map\country1.dbf').geometries()ax1.add_geometries(country, ccrs.PlateCarree(),facecolor='none', edgecolor='gray',zorder = 1,lw=0.5)ax1.set_title('title',loc='left',fontsize =15)ax1.set_xticks(np.arange(leftlon,rightlon+1,10))ax1.set_yticks(np.arange(lowerlat,upperlat+1,10))lon_formatter = cticker.LongitudeFormatter()lat_formatter = cticker.LatitudeFormatter()ax1.xaxis.set_major_formatter(lon_formatter)ax1.yaxis.set_major_formatter(lat_formatter)ax1.xaxis.set_minor_locator(mticker.MultipleLocator(5))#顯示x軸副刻度ax1.yaxis.set_minor_locator(mticker.MultipleLocator(5))#顯示x軸副刻度plt.tick_params(labelsize=15)cs = ax1.contourf(lon,lat,r, zorder=0 ,levels=np.arange(-1,1.1,0.1) \                        , extend = 'both',transform=ccrs.PlateCarree(), cmap=plt.cm.bwr)clip=maskout.shp2clip(cs,ax1,'country1','China')# ax1.scatter(lon,lat,s =2,color='black',transform=ccrs.PlateCarree())cf=ax1.contourf(lon,lat,p, [0,0.05,1],zorder=2,hatches=[ 'xxx',None],colors='none',)for collection in cf.collections: collection.set_edgecolor('green') collection.set_linewidth(0)clip=maskout.shp2clip(cf,ax1,'country1','China')axx=ax1.inset_axes([0,0,1,0.3],anchor=(1,0),projection=ccrs.PlateCarree())leftlon, rightlon, lowerlat, upperlat = (107,122,0,22)extend_southsea=[leftlon, rightlon, lowerlat, upperlat]axx.set_extent(extend_southsea, crs=ccrs.PlateCarree())china = shpreader.Reader(r'H:\prediction\map\bou2_4l.dbf').geometries()axx.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='gray',zorder = 1,lw=0.5)country=shpreader.Reader(r'H:\prediction\map\country1.dbf').geometries()axx.add_geometries(country, ccrs.PlateCarree(),facecolor='none', edgecolor='gray',zorder = 1,lw=0.5)css=axx.contourf(lon,lat,r, zorder=0 ,levels=np.arange(-1,1.1,0.1) \ , extend = 'both',transform=ccrs.PlateCarree(), cmap=plt.cm.bwr)clip=maskout.shp2clip2(css,axx,'country1','China')csf=axx.contourf(lon,lat,p, [0,0.05,1],zorder=2,hatches=['xxx',None],colors='none')for collection in csf.collections: collection.set_edgecolor('green') collection.set_linewidth(0)clip=maskout.shp2clip2(csf,axx,'country1','China')position=fig.add_axes([0.25, 0.05, 0.5, 0.03])cb=fig.colorbar(cs,cax=position,orientation='horizontal',format='%.1f',ticks=np.arange(-1,1.1,0.4),extendrect=True,extendfrac='auto')cb.ax.tick_params(labelsize=15)plt.savefig(r'test-ori.png',dpi=500)plt.show()

改進后的maskout.py:

#coding=utf-8########################################################################################################################################This module enables you to maskout the unneccessary data outside the interest region on a matplotlib-plotted output instance####################in an effecient way,You can use this script for free ##################################################################################################################################################################################################USAGE: INPUT include 'originfig':the matplotlib instance### 'ax': the Axes instance# 'shapefile': the shape file used for generating a basemap A# 'region':the name of a region of on the basemap A,outside the region the data is to be maskout# OUTPUT is 'clip' :the the masked-out or clipped matplotlib instance.import shapefilefrom matplotlib.path import Pathfrom matplotlib.patches import PathPatchfrom shapely.geometry import Point as ShapelyPointfrom shapely.geometry import Polygon as ShapelyPolygonfrom collections import Iterabledef shp2clip(originfig,ax,m,shpfile,region, clabel = None, vcplot = None): sf = shapefile.Reader(shpfile) vertices = [] codes = [] for shape_rec in sf.shapeRecords(): ####這里需要找到和region匹配的唯一標識符,record[]中必有一項是對應(yīng)的。 if shape_rec.record[3] == region: #####在country1.shp上,對中國以外的其他國家或地區(qū)進行maskout # if shape_rec.record[7] in region: #####在bou2_4p.shp上,對中國的某幾個省份或地區(qū)之外的部分進行maskout pts = shape_rec.shape.points prt = list(shape_rec.shape.parts) + [len(pts)] for i in range(len(prt) - 1): for j in range(prt[i], prt[i+1]): vertices.append(m(pts[j][0], pts[j][1])) codes += [Path.MOVETO] codes += [Path.LINETO] * (prt[i+1] - prt[i] -2) codes += [Path.CLOSEPOLY] clip = Path(vertices, codes)            clip = PathPatch(clip, transform=ax.transData)      if vcplot: if isinstance(originfig,Iterable): for ivec in originfig: ivec.set_clip_path(clip) else: originfig.set_clip_path(clip) else: for contour in originfig.collections:            contour.set_clip_path(clip) if clabel: clip_map_shapely = ShapelyPolygon(vertices) for text_object in clabel: if not clip_map_shapely.contains(ShapelyPoint(text_object.get_position())):                text_object.set_visible(False) return clip# 改進代碼def shp2clip2(originfig,ax,shpfile,region,clabel = None, vcplot = None): sf = shapefile.Reader(shpfile) vertices = [] codes = [] for shape_rec in sf.shapeRecords(): if shape_rec.record[3] == region: ####這里需要找到和region匹配的唯一標識符,record[]中必有一項是對應(yīng)的。 pts = shape_rec.shape.points prt = list(shape_rec.shape.parts) + [len(pts)] for i in range(len(prt) - 1): for j in range(prt[i], prt[i+1]): if pts[j][0]<107 or pts[j][0]>122 or pts[j][1]>22 or pts[j][1]<0: vertices.append((107, 22)) else: vertices.append((pts[j][0], pts[j][1])) codes += [Path.MOVETO] codes += [Path.LINETO] * (prt[i+1] - prt[i] -2) codes += [Path.CLOSEPOLY] clip = Path(vertices, codes) # print(clip) clip = PathPatch(clip, transform=ax.transData) for contour in originfig.collections: contour.set_clip_path(clip) if vcplot: if isinstance(originfig,Iterable): for ivec in originfig: ivec.set_clip_path(clip) else: originfig.set_clip_path(clip) else: for contour in originfig.collections:            contour.set_clip_path(clip) if clabel: clip_map_shapely = ShapelyPolygon(vertices) for text_object in clabel: if not clip_map_shapely.contains(ShapelyPoint(text_object.get_position())):                text_object.set_visible(False)            return clip


獲取本文Python白化南海小地圖數(shù)據(jù)和代碼的途徑,

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python氣象數(shù)據(jù)處理與繪圖:克里金(Kriging)插值與可視化
編程分享|利用Python可視化全球二氧化碳排放
Python可視化 | 使用xarray讀取tif數(shù)據(jù)并可視化
Python氣象繪圖(三十八)—地形缺測
WRF后處理 | Domain繪制優(yōu)化
Python 圖像處理簡介——色彩陰影調(diào)整
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服