python Cartopy的基礎(chǔ)使用詳解
前言
常用地圖底圖的繪制一般由Basemap或者cartopy模塊完成,由于Basemap庫(kù)是基于python2開(kāi)發(fā)的一個(gè)模塊,目前已經(jīng)不開(kāi)發(fā)維護(hù)。故簡(jiǎn)單介紹cartopy模塊的一些基礎(chǔ)操作。 一、基礎(chǔ)介紹
首先導(dǎo)入相關(guān)模塊。
import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature from 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í)設(shè)置坐標(biāo)軸標(biāo)簽改變?cè)摌?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用來(lái)設(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)
可以看到效果圖如下:
當(dāng)然如果想對(duì)坐標(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)閉該控制,然后開(kāi)啟常規(guī)坐標(biāo)軸設(shè)置。
二、區(qū)域地圖的繪制
當(dāng)我們?cè)谀骋恍K區(qū)域研究時(shí),需要繪制區(qū)域地圖。此時(shí)我們可以引入命令:
ax.set_extent(box,crs=ccrs.PlateCarree())
其中box為繪制區(qū)域,crs為投影類型。其他命令基本不變。設(shè)置box為[40,180,0,90],可得到效果圖如下:
總結(jié)
為方便各位讀者,我書寫了繪制地圖的函數(shù),大家在使用時(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用來(lái)設(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3使用logging包,如何把日志寫到系統(tǒng)的rsyslog中
這篇文章主要介紹了python3使用logging包,如何把日志寫到系統(tǒng)的rsyslog中的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python程序中調(diào)用其他程序的實(shí)現(xiàn)
本文主要介紹了python程序中調(diào)用其他程序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02使用Python實(shí)現(xiàn)將多表分批次從數(shù)據(jù)庫(kù)導(dǎo)出到Excel
這篇文章主要介紹了使用Python實(shí)現(xiàn)將多表分批次從數(shù)據(jù)庫(kù)導(dǎo)出到Excel,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05Python將list中的string批量轉(zhuǎn)化成int/float的方法
今天小編就為大家分享一篇Python將list中的string批量轉(zhuǎn)化成int/float的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Python網(wǎng)絡(luò)編程之ftplib模塊
這篇文章介紹了Python網(wǎng)絡(luò)編程之ftplib模塊,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Python中count統(tǒng)計(jì)指定字符、元素出現(xiàn)次數(shù)的實(shí)現(xiàn)
本文主要介紹了Python中count統(tǒng)計(jì)指定字符、元素出現(xiàn)次數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07