日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

python Cartopy的基础使用方法

發(fā)布時(shí)間:2023/12/15 综合教程 35 生活家
生活随笔 收集整理的這篇文章主要介紹了 python Cartopy的基础使用方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

前言

常用地圖底圖的繪制一般由Basemap或者cartopy模塊完成,由于Basemap庫(kù)是基于python2開發(fā)的一個(gè)模塊,目前已經(jīng)不開發(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用來設(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)閉該控制,然后開啟常規(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用來設(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

總結(jié)

以上是生活随笔為你收集整理的python Cartopy的基础使用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。