python绘制地图地图cartopy_python Cartopy的基础使用详解
前言
常用地圖底圖的繪制一般由Basemap或者cartopy模塊完成,由于Basemap庫是基于python2開發的一個模塊,目前已經不開發維護。故簡單介紹cartopy模塊的一些基礎操作。 一、基礎介紹
首先導入相關模塊。
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
首先介紹參數projection,該命令可以配合ccrs設置投影類型,此處以方形投影命令為示例。其中central_longitude參數為投影中心位置。其中心設置與Basemap設置規則一樣,詳情可以看上一篇文章。
ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
在設置好繪制類型后,繪制地圖各特征量。其代碼如下:
#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)
參數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)
可以看到效果圖如下:
當然如果想對坐標軸粗細變化可以引入一下命令。
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);####設置上部坐標軸的粗細
應該在該模塊下,控制坐標軸的命令已經和常規不一樣。因此先關閉該控制,然后開啟常規坐標軸設置。
二、區域地圖的繪制
當我們在某一小塊區域研究時,需要繪制區域地圖。此時我們可以引入命令:
ax.set_extent(box,crs=ccrs.PlateCarree())
其中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 Cartopy的基礎使用詳解的文章就介紹到這了,更多相關python Cartopy內容請搜索云海天教程以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持云海天教程!
原文鏈接:https://blog.csdn.net/weixin_49284189/article/details/109376547
總結
以上是生活随笔為你收集整理的python绘制地图地图cartopy_python Cartopy的基础使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 五大通信设备厂商:华为存隐忧,中兴已掉队
 - 下一篇: 【蜂口 | AI人工智能】表情识别——龙