溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python Cartopy的基礎(chǔ)使用方法

發(fā)布時間:2020-11-02 15:02:03 來源:億速云 閱讀:435 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)python Cartopy的基礎(chǔ)使用方法,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

前言

常用地圖底圖的繪制一般由Basemap或者cartopy模塊完成,由于Basemap庫是基于python2開發(fā)的一個模塊,目前已經(jīng)不開發(fā)維護(hù)。故簡單介紹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ì)。此處繪制海岸線和海洋,效果圖如下:

python Cartopy的基礎(chǔ)使用方法

在繪制結(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和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)

可以看到效果圖如下:

python Cartopy的基礎(chǔ)使用方法

當(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],可得到效果圖如下:

python Cartopy的基礎(chǔ)使用方法

總結(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

以上就是python Cartopy的基礎(chǔ)使用方法,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI