python Cartopy的基礎(chǔ)使用詳解
前言
常用地圖底圖的繪制一般由Basemap或者cartopy模塊完成,由于Basemap庫是基于python2開發(fā)的一個模塊,目前已經(jīng)不開發(fā)維護(hù)。故簡單介紹cartopy模塊的一些基礎(chǔ)操作。 一、基礎(chǔ)介紹
首先導(dǎo)入相關(guān)模塊。
import numpy as npimport matplotlib.pyplot as pltimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefrom cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
首先介紹參數(shù)projection,該命令可以配合ccrs設(shè)置投影類型,此處以方形投影命令為示例。其中central_longitude參數(shù)為投影中心位置。其中心設(shè)置與Basemap設(shè)置規(guī)則一樣,詳情可以看上一篇文章。
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
在設(shè)置好繪制類型后,繪制地圖各特征量。其代碼如下:
#ax.add_feature(cfeature.LAKES.with_scale(scale))ax.add_feature(cfeature.OCEAN.with_scale(scale))#ax.add_feature(cfeature.RIVERS.with_scale(scale))#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
參數(shù)scale為地圖分辨率,目前支持10m,50m,110m,參數(shù)lw為線條粗細(xì)。此處繪制海岸線和海洋,效果圖如下:
在繪制結(jié)束后,作為地圖。經(jīng)緯度自然是必不可少的,在該模塊中,引進(jìn)同時設(shè)置坐標(biāo)軸標(biāo)簽改變該標(biāo)簽刻度的表示,具體形式如下:
ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree())ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())#zero_direction_label用來設(shè)置經(jīng)度的0度加不加E和Wlon_formatter = LongitudeFormatter(zero_direction_label=False)lat_formatter = LatitudeFormatter()ax.xaxis.set_major_formatter(lon_formatter)ax.yaxis.set_major_formatter(lat_formatter)
可以看到效果圖如下:
當(dāng)然如果想對坐標(biāo)軸粗細(xì)變化可以引入一下命令。
ax.outline_patch.set_visible(False)ax.spines[’bottom’].set_visible(True)ax.spines[’left’].set_visible(True)ax.spines[’right’].set_visible(True)ax.spines[’top’].set_visible(True)ax.spines[’bottom’].set_linewidth(2.5);###設(shè)置底部坐標(biāo)軸的粗細(xì)ax.spines[’left’].set_linewidth(2.5);####設(shè)置左邊坐標(biāo)軸的粗細(xì)ax.spines[’right’].set_linewidth(2.5);###設(shè)置右邊坐標(biāo)軸的粗細(xì)ax.spines[’top’].set_linewidth(2.5);####設(shè)置上部坐標(biāo)軸的粗細(xì)
應(yīng)該在該模塊下,控制坐標(biāo)軸的命令已經(jīng)和常規(guī)不一樣。因此先關(guān)閉該控制,然后開啟常規(guī)坐標(biāo)軸設(shè)置。
二、區(qū)域地圖的繪制
當(dāng)我們在某一小塊區(qū)域研究時,需要繪制區(qū)域地圖。此時我們可以引入命令:
ax.set_extent(box,crs=ccrs.PlateCarree())
其中box為繪制區(qū)域,crs為投影類型。其他命令基本不變。設(shè)置box為[40,180,0,90],可得到效果圖如下:
總結(jié)
為方便各位讀者,我書寫了繪制地圖的函數(shù),大家在使用時可直接調(diào)用。此處示例為方形投影,若希望繪制其他投影。只需要修改函數(shù)部分參數(shù)即可。代碼如下:
def map_make(scale,box,xstep,ystep): ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) a = (box[1]-box[0])//xstep x_start = box[1] - a*xstep a = (box[3]-box[2])//ystep y_start = box[3] - a*ystep ax.set_extent(box,crs=ccrs.PlateCarree()) #ax.add_feature(cfeature.LAKES.with_scale(scale)) #ax.add_feature(cfeature.OCEAN.with_scale(scale)) #ax.add_feature(cfeature.RIVERS.with_scale(scale)) #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5) ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2) ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree()) ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree()) #zero_direction_label用來設(shè)置經(jīng)度的0度加不加E和W lon_formatter = LongitudeFormatter(zero_direction_label=False) lat_formatter = LatitudeFormatter() ax.xaxis.set_major_formatter(lon_formatter) ax.yaxis.set_major_formatter(lat_formatter) #添加網(wǎng)格線 ax.grid() ax.outline_patch.set_visible(False) ax.spines[’bottom’].set_visible(True) ax.spines[’left’].set_visible(True) ax.spines[’right’].set_visible(True) ax.spines[’top’].set_visible(True) ax.spines[’bottom’].set_linewidth(2.5);###設(shè)置底部坐標(biāo)軸的粗細(xì) ax.spines[’left’].set_linewidth(2.5);####設(shè)置左邊坐標(biāo)軸的粗細(xì) ax.spines[’right’].set_linewidth(2.5);###設(shè)置右邊坐標(biāo)軸的粗細(xì) ax.spines[’top’].set_linewidth(2.5);####設(shè)置上部坐標(biāo)軸的粗細(xì) return ax
到此這篇關(guān)于python Cartopy的基礎(chǔ)使用詳解的文章就介紹到這了,更多相關(guān)python Cartopy內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法2. ASP常用日期格式化函數(shù) FormatDate()3. 解析原生JS getComputedStyle4. 輕松學(xué)習(xí)XML教程5. HTML DOM setInterval和clearInterval方法案例詳解6. 阿里前端開發(fā)中的規(guī)范要求7. xpath簡介_動力節(jié)點Java學(xué)院整理8. css代碼優(yōu)化的12個技巧9. jsp實現(xiàn)登錄驗證的過濾器10. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進(jìn))
