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è)置投影類(lèi)型,此處以方形投影命令為示例。其中central_longitude參數(shù)為投影中心位置。其中心設(shè)置與Basemap設(shè)置規(guī)則一樣,詳情可以看上一篇文章。
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
在設(shè)置好繪制類(lèi)型后,繪制地圖各特征量。其代碼如下:
#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為線(xiàn)條粗細(xì)。此處繪制海岸線(xiàn)和海洋,效果圖如下:

在繪制結(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為投影類(lèi)型。其他命令基本不變。設(shè)置box為[40,180,0,90],可得到效果圖如下:

總結(jié)
為方便各位讀者,我書(shū)寫(xiě)了繪制地圖的函數(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)格線(xiàn) 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)文章
Python任務(wù)自動(dòng)化工具tox使用教程
這篇文章主要介紹了Python任務(wù)自動(dòng)化工具tox使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Python不使用庫(kù)進(jìn)行矩陣運(yùn)算詳解
這篇文章主要介紹了Python不使用庫(kù)進(jìn)行矩陣運(yùn)算詳解,矩陣乘法中,需要判斷兩個(gè)矩陣是否可以進(jìn)行相乘,即前一個(gè)矩陣的列是否等于后一個(gè)矩陣的行,,需要的朋友可以參考下2023-08-08
使用Python和python-pptx構(gòu)建Markdown到PowerPoint轉(zhuǎn)換器
在這篇博客中,我們將深入分析一個(gè)使用 Python 開(kāi)發(fā)的應(yīng)用程序,該程序可以將 Markdown 文件轉(zhuǎn)換為 PowerPoint 演示文稿,我們將探討代碼結(jié)構(gòu)、功能和關(guān)鍵組件,并解決一個(gè)特定的 bug,需要的朋友可以參考下2025-03-03
python爬蟲(chóng)lxml庫(kù)解析xpath網(wǎng)頁(yè)過(guò)程示例
這篇文章主要為大家介紹了python爬蟲(chóng)lxml庫(kù)解析xpath網(wǎng)頁(yè)的過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python編程快速上手——Excel到CSV的轉(zhuǎn)換程序案例分析
這篇文章主要介紹了Python Excel到CSV的轉(zhuǎn)換程序,結(jié)合具體案例形式分析了Python操作Excel到CSV轉(zhuǎn)換的操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02
使用Python實(shí)現(xiàn)圖像LBP特征提取的操作方法
LBP特征叫做局部二值模式,常用于紋理特征提取,并在紋理分類(lèi)中具有較強(qiáng)的區(qū)分能力,本文給大家介紹了如何使用Python實(shí)現(xiàn)圖像LBP特征提取的操作方法,需要的朋友可以參考下2025-04-04

