1. 程式人生 > 實用技巧 >零基礎小白python入門必看之Cartopy的基礎使用

零基礎小白python入門必看之Cartopy的基礎使用

文章目錄

  • 前言
  • 一、基礎介紹
  • 二、區域地圖的繪製
  • 總結

前言

常用地圖底圖的繪製一般由Basemap或者cartopy模組完成,由於Basemap庫是基於python2開發的一個模組,目前已經不開發維護。故簡單介紹cartopy模組的一些基礎操作。

如果大家在學習中遇到困難,想找一個python學習交流環境,可以加入我們的python圈,裙號930900780,可領取python學習資料,會節約很多時間,減少很多遇到的難題。

一、基礎介紹

首先匯入相關模組。

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
12345

首先介紹引數projection,該命令可以配合ccrs設定投影型別,此處以方形投影命令為示例。其中central_longitude引數為投影中心位置。其中心設定與Basemap設定規則一樣,詳情可以看上一篇文章。

ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
1

在設定好繪製型別後,繪製地圖各特徵量。其程式碼如下:

#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)
12345

引數scale為地圖解析度,目前支援10m,50m,110m,引數lw為線條粗細。此處繪製海岸線和海洋,效果圖如下:


在繪製結束後,作為地圖。經緯度自然是必不可少的,在該模組中,引進同時設定座標軸標籤改變該標籤刻度的表示,具體形式如下:

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用來設定經度的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)
1234567

可以看到效果圖如下:


當然如果想對座標軸粗細變化可以引入一下命令。

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);###設定底部座標軸的粗細
ax.spines['left'].set_linewidth(2.5);####設定左邊座標軸的粗細
ax.spines['right'].set_linewidth(2.5);###設定右邊座標軸的粗細
ax.spines['top'].set_linewidth(2.5);####設定上部座標軸的粗細
   
12345678910

應該在該模組下,控制座標軸的命令已經和常規不一樣。因此先關閉該控制,然後開啟常規座標軸設定。

二、區域地圖的繪製

當我們在某一小塊區域研究時,需要繪製區域地圖。此時我們可以引入命令:

ax.set_extent(box,crs=ccrs.PlateCarree())
1

其中box為繪製區域,crs為投影型別。其他命令基本不變。設定box為[40,180,0,90],可得到效果圖如下:

總結

為方便各位讀者,我書寫了繪製地圖的函式,大家在使用時可直接呼叫。此處示例為方形投影,若希望繪製其他投影。只需要修改函式部分引數即可。程式碼如下:

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用來設定經度的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)
    #新增網格線
    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);###設定底部座標軸的粗細
    ax.spines['left'].set_linewidth(2.5);####設定左邊座標軸的粗細
    ax.spines['right'].set_linewidth(2.5);###設定右邊座標軸的粗細
    ax.spines['top'].set_linewidth(2.5);####設定上部座標軸的粗細
    
    return ax

最後多說一句,想學習Python可聯絡小編,這裡有我自己整理的整套python學習資料和路線,想要這些資料的都可以進q裙930900780領取。

本文章素材來源於網路,如有侵權請聯絡刪除。