Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼_第1頁
Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼_第2頁
Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼_第3頁
Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼_第4頁
Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Python實(shí)現(xiàn)繪制Matlab格式的地圖邊框的示例代碼目錄1、Python繪制色斑圖2、Python繪制比例尺、指南針3、Python繪制Matlab格式的地圖邊框

1、Python繪制色斑圖

importmatplotlib.pyplotasplt

importnumpyasnp

fromcartopy.mpl.tickerimportLongitudeFormatter,LatitudeFormatter

importcartopy.crsasccrs

importcartopy.featureascfeature

importpandasaspd

importmaskout

importmatplotlib.patchesasmpatches

fromcartopy.io.shapereaderimportReader

frommatplotlibimportrcParams

config={"font.family":'TimesNewRoman',"font.size":16,"mathtext.fontset":'stix'}

rcParams.update(config)

df1=pd.read_excel(r"F:/Rpython/lp37/henanmap/henan.xlsx")

lat=df1['lat']

lon=df1['lon']

PM25=df1['PM25']

#設(shè)置經(jīng)緯度并用scipy包中的函數(shù)插值

fromerpolateimportRbf

olon=np.linspace(110,117,100)

olat=np.linspace(31,37,100)

olon,olat=np.meshgrid(olon,olat)

#插值處理,‘linear',‘nearest',‘cubic'

#cubic,gaussian,inverse_multiquadric,linear,multiquadric,quintic,thin_plate

#rain_data_new=griddata((lon,lat),data,(olon,olat),method='linear')

func1=Rbf(lon,lat,PM25,function='linear')

PM25=func1(olon,olat)

print(olon)

print(olat)

print(PM25)

print(olat.shape)

print(PM25.shape)

#建立畫布

fig2=plt.figure(figsize=(16,12))

proj=ccrs.PlateCarree()

leftlon,rightlon,lowerlat,upperlat=(110,117,31,37)#根據(jù)上下限確定范圍,至少為10°

#春------------------------------------------------------------------------------------

#左底寬高

ax=fig2.add_subplot(1,1,1,projection=ccrs.PlateCarree())

#在畫布的絕對坐標(biāo)建立子圖

ax.set_extent([leftlon,rightlon,lowerlat,upperlat],crs=ccrs.PlateCarree())

#設(shè)置地圖屬性:加載國界、海岸線,land為灰色

ax.stock_img()

#繪制河南省行政邊界

ticks=np.arange(30,80,5)

cf=ax.contourf(olon,olat,PM25,levels=ticks,cmap='gist_rainbow',transform=ccrs.PlateCarree(),extend='both')

clip1=maskout.shp2clip(cf,ax,'F:/Rpython/lp37/henanmap/henan1')

cf=plt.colorbar(cf,ticks=ticks,shrink=0.96,orientation='vertical',extend='both',pad=0.01,aspect=35)

ax.add_feature(cfeature.COASTLINE.with_scale('50m'),linewidth=0.5,zorder=2,color='k')#添加海岸線

ax.add_feature(cfeature.LAKES.with_scale('50m'))

ax.add_feature(cfeature.RIVERS.with_scale('50m'))

ax.add_feature(cfeature.OCEAN.with_scale('50m'))

ax.add_feature(cfeature.LAND.with_scale('50m'))

ax.add_geometries(Reader(r'F:/Rpython/lp37/henanmap/henan1.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=1.2,zorder=1)

ax.add_geometries(Reader(r'F:/Rpython/lp27/data/river1.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='b',linewidth=0.2)

ax.add_geometries(Reader(r'F:/Rpython/lp27/data/china1.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='r',linewidth=1.2)

ax.add_geometries(Reader(r'F:/Rpython/lp27/data/china2.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='r',linewidth=0.8)

ax.add_geometries(Reader(r'F:/Rpython/lp27/data/ne_50m_lakes.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=0.2)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/1級河流.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='RoyalBlue',linewidth=0.4)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/2級河流.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='DodgerBlue',linewidth=0.3)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/3級河流.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='DeepSkyBlue',linewidth=0.2)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/4級河流.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='SkyBlue',linewidth=0.15)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/5級河流.shp').geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='LightSkyBlue',linewidth=0.05)

ax.add_geometries(Reader(r'F:/Rpython/lp37/data37/river/主要湖泊.shp').geometries(),ccrs.PlateCarree(),edgecolor='none',linewidth=0,facecolor='#BEE8FF')

#以下6條語句是定義地理坐標(biāo)標(biāo)簽格式

ax.set_xticks(np.arange(leftlon,rightlon+0.1,1),crs=ccrs.PlateCarree())

ax.set_yticks(np.arange(lowerlat,upperlat+0.1,1),crs=ccrs.PlateCarree())

lon_formatter=LongitudeFormatter()

lat_formatter=LatitudeFormatter()

ax.xaxis.set_major_formatter(lon_formatter)

ax.yaxis.set_major_formatter(lat_formatter)

plt.savefig('F:/Rpython/lp37/plot185.4.png',dpi=600,bbox_inches='tight',pad_inches=0)

plt.show()

2、Python繪制比例尺、指南針

#添加比例尺,指南針

defadd_north(ax,labelsize=18,loc_x=0.95,loc_y=0.99,width=0.06,height=0.09,pad=0.14):

畫一個(gè)比例尺帶'N'文字注釋

主要參數(shù)如下

:paramax:要畫的坐標(biāo)區(qū)域Axes實(shí)例plt.gca()獲取即可

:paramlabelsize:顯示'N'文字的大小

:paramloc_x:以文字下部為中心的占整個(gè)ax橫向比例

:paramloc_y:以文字下部為中心的占整個(gè)ax縱向比例

:paramwidth:指南針占ax比例寬度

:paramheight:指南針占ax比例高度

:parampad:文字符號占ax比例間隙

:return:None

minx,maxx=ax.get_xlim()

miny,maxy=ax.get_ylim()

ylen=maxy-miny

xlen=maxx-minx

left=[minx+xlen*(loc_x-width*.5),miny+ylen*(loc_y-pad)]

right=[minx+xlen*(loc_x+width*.5),miny+ylen*(loc_y-pad)]

top=[minx+xlen*loc_x,miny+ylen*(loc_y-pad+height)]

center=[minx+xlen*loc_x,left[1]+(top[1]-left[1])*.4]

triangle=mpatches.Polygon([left,top,right,center],color='k')

ax.text(s='N',

x=minx+xlen*loc_x,

y=miny+ylen*(loc_y-pad+height),

fontsize=labelsize,

horizontalalignment='center',

verticalalignment='bottom')

ax.add_patch(triangle)

#-----------函數(shù):添加比例尺--------------

defadd_scalebar(ax,lon0,lat0,length,size=0.01):

ax:坐標(biāo)軸

lon0:經(jīng)度

lat0:緯度

length:長度

size:控制粗細(xì)和距離的

#style3

ax.hlines(y=lat0,xmin=lon0,xmax=lon0+length/111,colors="black",ls="-",lw=1,linewidth=3,label='%dkm'%(length))

ax.vlines(x=lon0,ymin=lat0-size,ymax=lat0+size,colors="black",ls="-",lw=1,linewidth=3)

ax.vlines(x=lon0+length/2/111,ymin=lat0-size,ymax=lat0+size,colors="black",ls="-",lw=1,linewidth=3)

ax.vlines(x=lon0+length/111,ymin=lat0-size,ymax=lat0+size,colors="black",ls="-",lw=1,linewidth=3)

ax.text(lon0+length/111,lat0+size+0.05,'%d'%(length),horizontalalignment='center')

ax.text(lon0+length/2/111,lat0+size+0.05,'%d'%(length/2),horizontalalignment='center')

ax.text(lon0,lat0+size+0.05,'0',horizontalalignment='center')

ax.text(112.28,31.45,'km',horizontalalignment='center')

3、Python繪制Matlab格式的地圖邊框

defdrow_the_scale(y,x,text,length=1.5,lw=5):

#畫比例尺函數(shù)

#y代表比例尺所在緯度

#x代表比例尺開始的經(jīng)度

#text代表比例尺最后刻度值

#length代表比例尺的長度,單位為多少個(gè)經(jīng)度

#lw代表比例尺的寬度

step=length/5#計(jì)算步長,畫五格

#畫黑白線五條

plt.hlines(y=y,xmin=x,xmax=x+step,colors="black",ls="-",lw=lw)

plt.hlines(y=y,xmin=x+step,xmax=x+step*2,colors="white",ls="-",lw=lw)

plt.hlines(y=y,xmin=x+step*2,xmax=x+step*3,colors="black",ls="-",lw=lw)

plt.hlines(y=y,xmin=x+step*3,xmax=x+step*4,colors="white",ls="-",lw=lw)

plt.hlines(y=y,xmin=x+step*4,xmax=x+step*5,colors="black",ls="-",lw=lw)

#畫長刻度兩個(gè)

plt.vlines(x=x,ymin=y-(lw/100)*3,ymax=y+lw/100,colors="black",ls="-",lw=1)

plt.vlines(x=x+length,ymin=y-(lw/100)*3,ymax=y+lw/100,colors="black",ls="-",lw=1)

#畫段刻度四個(gè)

plt.vlines(x=x+step,ymin=y-(lw/100)*2,ymax=y+lw/100,colors="black",ls="-",lw=1)

plt.vlines(x=x+step*2,ymin=y-(lw/100)*2,ymax=y+lw/100,colors="black",ls="-",lw=1)

plt.vlines(x=x+step*3,ymin=y-(lw/100)*2,ymax=y+lw/100,colors="black",ls="-",lw=1)

plt.vlines(x=x+step*4,ymin=y-(lw/100)*2,ymax=y+lw/100,colors="black",ls="-",lw=1)

#寫字,0,500,km

plt.text(x,y-(lw/100)*7,'0',horizontalalignment='center')

plt.text(x+length,y-(lw/100)*7,text,horizontalalignment='center')

plt.text(x+length/2,y+(lw/100)*2,'km',horizontalalignment='center')

defdrowscale(extent,scale_y,scale_x,scale_text,step=5,lw=10,scale_length=1.5,scale_lw=5):

#畫地圖黑白邊框和比例尺

#extent:表示四周經(jīng)緯度[west,east,south,north]

#scale_y,scale_x,scale_text:代表比例尺的位置,緯度,經(jīng)度,刻度值

#step:表示步長,一格代表幾個(gè)經(jīng)緯度

#lw:代表邊框

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論