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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python用cartopy包画地图_利用Cartopy绘制带有地图投影的图形

發布時間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python用cartopy包画地图_利用Cartopy绘制带有地图投影的图形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

從NCL轉向python繪圖后,最迷茫的一點恐怕就是地理繪圖了,網絡上流傳了大量的python繪圖教程,但是很少是關于地理方面的,包括各類投影坐標系,地理地形,界線shp的繪制等等。這篇文章以Cartopy為主,介紹如何通過Matplotlib結合Cartopy來靈活的繪制帶有地圖投影的圖像。

本部分需要使用Cartopy庫,通過:

1

2

3conda install -c conda-forge cartopy

#or

pip install cartopy

安裝。

一、Cartopy投影坐標系

在繪圖前首先要選擇適合的投影方式,目前cartopy提供了超過三十種的投影方式,其中也包含我們所熟悉的極地投影,墨卡托投影,蘭伯特投影等等。下面,將介紹幾種常用的投影方式的設置及使用。

1、 PlateCarree (無坐標轉換)

PlateCarree是一種基礎的投影方式,其計算方法是將經緯度網格設置成等距網格,實際上數據并沒有進行坐標轉換。

1cartopy.crs.PlateCarree(central_longitude=0.0)

參數: central_longitude : GeoAxes中心的經度,默認為0°

1

2

3

4

5

6

7

8

9import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature

fig = plt.figure(figsize=[10, 5])

ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.PlateCarree())

ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.PlateCarree(central_longitude = 120))

ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))

ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))

plt.show()

2、圓錐蘭伯特投影 LambertConformal

LambertConformal(正形蘭伯特投影),也就是常說的正軸圓錐投影。

1cartopy.crs.LambertConformal(central_longitude=-96.0, central_latitude=39.0, false_easting=0.0, false_northing=0.0, secant_latitudes=None, standard_parallels=None, cutoff=-30)

參數: central_longitude : GeoAxes中心的經度,默認為96°W

central_latitude :GeoAxes中心的緯度,默認為39°N

false_easting : 東偽偏移,默認為0m

false_northing :北偽偏移,默認為0m

standard_parallels : 標準平行緯度,默認為(33,45)

cutoff : 截止緯度,即最南邊界緯度,默認為30°S

1

2

3

4

5

6

7

8

9import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature

fig = plt.figure(figsize=[10, 5])

ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.LambertConformal())

ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.LambertConformal(cutoff=0))

ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))

ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))

plt.show()

3、墨卡托投影 Mercator1cartopy.crs.Mercator(central_longitude=0.0, min_latitude=-80.0, max_latitude=84.0, latitude_true_scale=None, false_easting=0.0, false_northing=0.0)

參數:

central_longitude : GeoAxes中心的經度,默認為0°

min_latitude :GeoAxes最南緯度,默認為80°S

max_latitude :GeoAxes最北緯度,默認為84°N

latitude_true_scale :比例尺為1的緯度,默認為赤道

false_easting : 東偽偏移,默認為0m

false_northing :北偽偏移,默認為0m

1

2

3

4

5

6

7import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature

fig = plt.figure(figsize=[10, 5])

ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.Mercator())

ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))

plt.show()

4、極地投影

NorthPolarStereo(北半球極射赤面投影)和SouthPolarStereo(南半球極射赤面投影)

1

2cartopy.crs.NorthPolarStereo(central_longitude=0.0, true_scale_latitude=None)

cartopy.crs.SouthPolarStereo(central_longitude=0.0, true_scale_latitude=None)

參數:

central_longitude : GeoAxes中心的經度,默認為0°

true_scale_latitude :比例尺為1的緯度。

1

2

3

4

5

6

7

8

9

10

11import matplotlib.pyplot as plt

import cartopy.crs as ccrs

import cartopy.feature as cfeature

fig = plt.figure(figsize=[10, 5])

ax1 = fig.add_subplot(1, 2, 1, projection=ccrs.NorthPolarStereo())

ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.SouthPolarStereo())

ax1.gridlines()

ax2.gridlines()

ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))

ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))

plt.show()

二、GeoAxes繪制

上篇文章講到Axes的構成,GeoAxes簡單講就是在Axes的基礎上添加了地理要素(地圖投影)。那么繪制GeoAxes有哪些不同呢?

我們以一個500hPa高度場的繪制為例。應大家要求,這次我裁剪了數據,控制了數據的大小,以方便大家下載。

神秘代碼:k1qd

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33import cartopy.crs as ccrs

import cartopy.feature as cfeature

import matplotlib.pyplot as plt

import cartopy.mpl.ticker as cticker

import xarray as xr

###數據讀取###

f = xr.open_dataset('./data/1980060106.nc')

z = f['z'].loc[:,500,:,:][0,:,:]/98

lat = z['latitude']

lon = z['longitude']

###建立Figure###

fig = plt.figure(figsize=(12,8))

###指定投影為中心經度為90°的PlateCarree###

ax = fig.add_subplot(1,1,1,projection = ccrs.PlateCarree(central_longitude=90))

###設置GeoAxes范圍###

ax.set_extent([0,180,0,90], crs=ccrs.PlateCarree())

###添加海岸線###

ax.add_feature(cfeature.COASTLINE.with_scale('50m'))

###設置坐標及刻度###

ax.set_xticks(np.arange(0,180+30,30), crs=ccrs.PlateCarree())

ax.set_yticks(np.arange(0,90+30,30), crs=ccrs.PlateCarree())

lon_formatter = cticker.LongitudeFormatter()

lat_formatter = cticker.LatitudeFormatter()

ax.xaxis.set_major_formatter(lon_formatter)

ax.yaxis.set_major_formatter(lat_formatter)

###圖題###

ax.set_title('500hPa Z',loc='center',fontsize=18)

ax.set_title('unit: dagpm',loc='right',fontsize=18)

###繪制等高線,指定坐標轉換方式###

c = ax.contour(lon,lat, z, zorder=0,transform=ccrs.PlateCarree())

###添加等高線標簽###

ax.clabel(c, fontsize=9, inline=1,fmt='%d')

plt.show()

這里對腳本中一些函數進行額外解釋:

在進行建立Axes時,要指明目標投影,使之成為GeoAxes

設置GeoAxes范圍時,輸入范圍的左右下上邊界經緯度

繪制等高線,指定坐標轉換方式,此示例是以最為常用的PlateCarree形式展示

大家可以使用上邊介紹的其它投影格式進行替換,繪圖時必須要弄清楚什么地方應該設置為數據的投影方式,什么地方設置為目標的投影方式。

總結

以上是生活随笔為你收集整理的python用cartopy包画地图_利用Cartopy绘制带有地图投影的图形的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。