气象常用库 | cartopy常用用法总结

2022-11-14 16:12:30 浏览数 (1)

1.导入cartopy库,认识一下常用的子模块

代码语言:javascript复制
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.io.shapereader import Reader
from cartopy.feature.nightshade import Nightshade
代码语言:javascript复制
# 这些是其余要用到的库
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import numpy as np
import datetime

一、绘制地图底图

2.指定投影方式为等经纬度投影,绘制默认海岸线底图

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()

3.在上一题的地图中,重新指定中心经度为180°

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines()

4.在上一题的地图中,指定绘制默认海岸线的线宽为0.3

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))

ax.coastlines(lw=0.3)

5.在中心经度为180°的等经纬度投影上,添加陆地

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))

ax.add_feature(cfeature.LAND)

6.在上一题基础上,添加海洋

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)

ax.add_feature(cfeature.OCEAN)

7.在上一题基础上,添加海岸线,并指定线宽为0.5

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)

ax.add_feature(cfeature.COASTLINE,lw=0.5)

8.在上一题基础上,添加河流,并指定线宽为1.0

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)

ax.add_feature(cfeature.RIVERS,lw=1.0)

9.在上一题基础上,添加湖泊,并指定颜色为red

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)

ax.add_feature(cfeature.LAKES,color='r')

10.在上一题基础上,添加国境线,并指定线宽为0.1(不推荐使用,因为该默认参数会使得我国部分领土丢失)

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')

ax.add_feature(cfeature.BORDERS,lw=0.1)

11.在第9题的基础上,设置想要显示的经纬度标签值,x轴为(-180,-120,-60,0,60,120,180),y轴为(-90,-60, -30, 0, 30, 60,90)

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')

ax.set_xticks([ 0,60,120,180,240,300,360], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())

12.在上一题的基础上,将经纬度标签转换为具有单位的形式

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ 0,60,120,180,240,300,360], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())

lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)

13.在上题的基础上,将刻度设置为灰色,并将其指向朝内

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.tick_params(color = 'gray',direction='in')

14.在上一题的基础上,选取经度为0°E-180°E,纬度为0°N-90°N的区域

代码语言:javascript复制
#方法一
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.set_extent([0,180,0,90],crs = ccrs.PlateCarree()) 
代码语言:javascript复制
#方法二
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.set_xlim(-180,0)
ax.set_ylim(0,90)

15.在上一题的基础上,选择绘图的精度为50m,使得呈现出更多细节(第一次绘制会下载数据资源,时间大概需要几分钟,后面就不需要了)

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND.with_scale('50m'))
ax.add_feature(cfeature.OCEAN.with_scale('50m'))
ax.add_feature(cfeature.COASTLINE.with_scale('50m'),lw=0.5)
ax.add_feature(cfeature.RIVERS.with_scale('50m'),lw=1.0)
ax.add_feature(cfeature.LAKES.with_scale('50m'),color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.set_extent([0,180,0,90],crs = ccrs.PlateCarree())

16.为第13题的图添加上虚线网格线

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.tick_params(color = 'gray',direction='in')

ax.gridlines(linestyle='--')

17.为上一题的图添加上标题“Cartopy”

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')
ax.set_xticks([ -180,-120,-60,0,60,120,180], crs=ccrs.PlateCarree())
ax.set_yticks([ -90,-60, -30, 0, 30, 60,90], crs=ccrs.PlateCarree())
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.tick_params(color = 'gray',direction='in')
ax.gridlines(linestyle='--')

ax.set_title('Cartopy')

二、自定义地图形状

18.设置一个梯形边界信息:纬度为60°S-60°N,上经度为40°E和140°E,下经度为20°E和160°E

代码语言:javascript复制
vertices = [(20, -60), (40, 60), (140, 60), (160, -60), (20, -60)]#梯形的四个点,首尾相连。如第一个点为左下角的点,经度为20°N,纬度为60°S
boundary = mpath.Path(vertices)
boundary
代码语言:javascript复制
Path(array([[ 20., -60.],
       [ 40.,  60.],
       [140.,  60.],
       [160., -60.],
       [ 20., -60.]]), None)

19.在等经纬度投影中,截取出上一题的梯形地图,略去标签信息

代码语言:javascript复制
fig = plt.figure(figsize=(4, 4), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE,lw=0.5)
ax.add_feature(cfeature.RIVERS,lw=1.0)
ax.add_feature(cfeature.LAKES,color='r')

ax.set_boundary(boundary, transform=ccrs.PlateCarree())
ax.set_extent([20,160,-60,60])

20.指定投影方式为兰伯特等角圆锥投影,并将中心经度设置为105°E,中心纬度设置为40°N

代码语言:javascript复制
fig=plt.figure(figsize=(4,4),dpi=200)
ax=plt.axes(projection=ccrs.LambertConformal(central_longitude=105, central_latitude=40))
ax.add_feature(cfeature.LAND)

21.设定扇形边界信息:经度范围为65°E-145°E,纬度范围为10°N到60°N

代码语言:javascript复制
vertices = [(65, 10), (65, 60),(145, 60), (145, 10), (65, 10)]
boundary = mpath.Path(vertices)

22.在第20题的图中截取上一题的扇形区域

代码语言:javascript复制
fig=plt.figure(figsize=(4,4),dpi=200)
ax=plt.axes(projection=ccrs.LambertConformal(central_longitude=105, central_latitude=40))
ax.add_feature(cfeature.LAND)

ax.set_boundary(boundary, transform=ccrs.PlateCarree())
ax.set_extent([65,145,0,60])
代码语言:javascript复制
#进阶用法,通过增加点数使得边缘变得平滑
fig=plt.figure(figsize=(4,4),dpi=200)
ax=plt.axes([0,0,1,1],projection=ccrs.LambertConformal(central_longitude=105, central_latitude=40))
ax.add_feature(cfeature.LAND)#添加陆地
latmin = 10
latmax = 60
lonmin = 70
lonmax = 140
lats = np.linspace(latmax, latmin, latmax - latmin   1)
lons = np.linspace(lonmin, lonmax, lonmax - lonmin   1)
vertices = [(lon, latmin) for lon in range(lonmin, lonmax   1, 1)]   [(lon, latmax) for lon in range(lonmax, lonmin - 1, -1)]
boundary = mpath.Path(vertices)
ax.set_boundary(boundary, transform=ccrs.PlateCarree())
ax.set_extent([lonmin,lonmax,latmin,latmax])

23.指定投影方式为极地立体投影

代码语言:javascript复制
fig = plt.figure(figsize=(4,4),dpi=200)
ax = plt.axes(projection=ccrs.SouthPolarStereo())

ax.gridlines()

ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)

24.设定半圆的边界信息:以(0.5,0.5)为圆心,0.5为半径(注:整个圆的直径默认为1)

代码语言:javascript复制
theta = np.linspace(0, np.pi, 100) #如果换成0.5*np.pi,就是四分之一圆了
center, radius = [0.5, 0.5], 0.5  #设定圆心和半径,范围是0-1
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius   center)

25.在23题的图中截取上一题的半圆形区域

代码语言:javascript复制
fig = plt.figure(figsize=(4,4),dpi=200)
ax = plt.axes(projection=ccrs.SouthPolarStereo())
ax.gridlines()
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)

ax.set_boundary(circle, transform=ax.transAxes)

26.在上一题的基础上,加粗自定义的边界

代码语言:javascript复制
fig = plt.figure(figsize=(4,4),dpi=200)
ax = plt.axes(projection=ccrs.SouthPolarStereo())
ax.gridlines()
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.set_boundary(circle, transform=ax.transAxes)

ax.outline_patch.set_linewidth(3)  

三、添加自定义的shape文件

27.设置shape文件的路径

代码语言:javascript复制
shp_path=r'/home/china-shapefiles/china-shapefiles/china.shp'

28.设定投影方式为等经纬度投影,并添加经纬度标签

代码语言:javascript复制
fig = plt.figure(figsize=(2, 2), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_xticks([ 70,90,110,130], crs=ccrs.PlateCarree())
ax.set_yticks([ 0,20,40,60], crs=ccrs.PlateCarree())
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.set_extent([70,140,0,60], crs=ccrs.PlateCarree())

29.在上一题的基础上,读取中国shp文件中的地理信息,并添加到上一题的地图底图中

代码语言:javascript复制
#方法一
fig = plt.figure(figsize=(2, 2), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_xticks([ 70,90,110,130], crs=ccrs.PlateCarree())
ax.set_yticks([ 0,20,40,60], crs=ccrs.PlateCarree())
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.set_extent([70,140,0,60], crs=ccrs.PlateCarree())

ax.add_geometries(Reader(shp_path).geometries(), ccrs.PlateCarree(), edgecolor='k', facecolor='g',linewidth=0.3)
代码语言:javascript复制
#方法二
fig = plt.figure(figsize=(2, 2), dpi=200)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_xticks([ 70,90,110,130], crs=ccrs.PlateCarree())
ax.set_yticks([ 0,20,40,60], crs=ccrs.PlateCarree())
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.set_extent([70,140,0,60], crs=ccrs.PlateCarree())

china = cfeature.ShapelyFeature(Reader(shp_path).geometries(), ccrs.PlateCarree(), edgecolor='k', facecolor='g', linewidth=0.3)
ax.add_feature(china)

四、填色图

30.生成纬度数据,从-90°~90°,分辨率为1°

代码语言:javascript复制
nlats=181
lats = np.linspace(-90, 90 ,nlats)

31.生成经度数据,从-180°~180°,分辨率为1°

代码语言:javascript复制
nlons=361
lons = np.linspace(-180, 180 ,nlons)
lons
代码语言:javascript复制
array([-180., -179., -178., -177., -176., -175., -174., -173., -172.,
       -171., -170., -169., -168., -167., -166., -165., -164., -163.,
       -162., -161., -160., -159., -158., -157., -156., -155., -154.,
       -153., -152., -151., -150., -149., -148., -147., -146., -145.,
       -144., -143., -142., -141., -140., -139., -138., -137., -136.,
       -135., -134., -133., -132., -131., -130., -129., -128., -127.,
       -126., -125., -124., -123., -122., -121., -120., -119., -118.,
       -117., -116., -115., -114., -113., -112., -111., -110., -109.,
       -108., -107., -106., -105., -104., -103., -102., -101., -100.,
        -99.,  -98.,  -97.,  -96.,  -95.,  -94.,  -93.,  -92.,  -91.,
        -90.,  -89.,  -88.,  -87.,  -86.,  -85.,  -84.,  -83.,  -82.,
        -81.,  -80.,  -79.,  -78.,  -77.,  -76.,  -75.,  -74.,  -73.,
        -72.,  -71.,  -70.,  -69.,  -68.,  -67.,  -66.,  -65.,  -64.,
        -63.,  -62.,  -61.,  -60.,  -59.,  -58.,  -57.,  -56.,  -55.,
        -54.,  -53.,  -52.,  -51.,  -50.,  -49.,  -48.,  -47.,  -46.,
        -45.,  -44.,  -43.,  -42.,  -41.,  -40.,  -39.,  -38.,  -37.,
        -36.,  -35.,  -34.,  -33.,  -32.,  -31.,  -30.,  -29.,  -28.,
        -27.,  -26.,  -25.,  -24.,  -23.,  -22.,  -21.,  -20.,  -19.,
        -18.,  -17.,  -16.,  -15.,  -14.,  -13.,  -12.,  -11.,  -10.,
         -9.,   -8.,   -7.,   -6.,   -5.,   -4.,   -3.,   -2.,   -1.,
          0.,    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.,   33.,   34.,   35.,
         36.,   37.,   38.,   39.,   40.,   41.,   42.,   43.,   44.,
         45.,   46.,   47.,   48.,   49.,   50.,   51.,   52.,   53.,
         54.,   55.,   56.,   57.,   58.,   59.,   60.,   61.,   62.,
         63.,   64.,   65.,   66.,   67.,   68.,   69.,   70.,   71.,
         72.,   73.,   74.,   75.,   76.,   77.,   78.,   79.,   80.,
         81.,   82.,   83.,   84.,   85.,   86.,   87.,   88.,   89.,
         90.,   91.,   92.,   93.,   94.,   95.,   96.,   97.,   98.,
         99.,  100.,  101.,  102.,  103.,  104.,  105.,  106.,  107.,
        108.,  109.,  110.,  111.,  112.,  113.,  114.,  115.,  116.,
        117.,  118.,  119.,  120.,  121.,  122.,  123.,  124.,  125.,
        126.,  127.,  128.,  129.,  130.,  131.,  132.,  133.,  134.,
        135.,  136.,  137.,  138.,  139.,  140.,  141.,  142.,  143.,
        144.,  145.,  146.,  147.,  148.,  149.,  150.,  151.,  152.,
        153.,  154.,  155.,  156.,  157.,  158.,  159.,  160.,  161.,
        162.,  163.,  164.,  165.,  166.,  167.,  168.,  169.,  170.,
        171.,  172.,  173.,  174.,  175.,  176.,  177.,  178.,  179.,
        180.])

32.将刚刚生成的经纬度数据整合成网格数据

代码语言:javascript复制
lons, lats = np.meshgrid(lons, lats)
lons
代码语言:javascript复制
array([[-180., -179., -178., ...,  178.,  179.,  180.],
       [-180., -179., -178., ...,  178.,  179.,  180.],
       [-180., -179., -178., ...,  178.,  179.,  180.],
       ...,
       [-180., -179., -178., ...,  178.,  179.,  180.],
       [-180., -179., -178., ...,  178.,  179.,  180.],
       [-180., -179., -178., ...,  178.,  179.,  180.]])

33.生成演示用的data,形状为(181,361),大小为0~181*361,间隔为1

代码语言:javascript复制
data=np.arange(0,181*361,1).reshape(181,361)

34.指定投影为等经纬度投影,将数据填充进去

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5))
ax = plt.axes(projection=ccrs.PlateCarree())


ax.contourf(lons, lats, data,transform=ccrs.PlateCarree())
ax.coastlines()

五、风场

35.随机在x,y处生成风速(x、y为坐标,u、v为x方向风速和y方向风速) 注:此处只是演示用,不必掌握,因为到时候大家的风场数据是从网站上或者模式中获得的,并不需要自己生成。

代码语言:javascript复制
shape=(20, 30)

x = np.linspace(200, 391.1, shape[1])
y = np.linspace(-50, 50, shape[0])

x2d, y2d = np.meshgrid(x, y)
u = 10 * (2 * np.cos(2 * np.deg2rad(x2d)   3 * np.deg2rad(y2d   30)) ** 2)
v = 20 * np.cos(6 * np.deg2rad(x2d))

36.指定等经纬度投影,绘制全球地图信息

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)  
ax = plt.axes(projection=ccrs.PlateCarree())

ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')

ax.set_global()

37.在上一题的地图底图上使用quiver函数绘制风场

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')
ax.set_global()

ax.quiver(x2d, y2d, u, v, transform=ccrs.PlateCarree())

六、流场

38.随机在x,y处生成风速(x、y为坐标,u、v为x方向风速和y方向风速)

代码语言:javascript复制
shape=(20, 30)

x = np.linspace(1, 61, shape[1])
y = np.linspace(1, 41, shape[0])

x2d, y2d = np.meshgrid(x, y)
u = 10 * ( np.cos(x2d)   3 * np.sin(y2d)   30)
v = 20 * x2d

39.在等经纬度投影的底图上绘制流场,指定线宽为2,密度为4,颜色根据风速大小改变

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)  
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND, edgecolor='black')


magnitude = (u ** 2   v ** 2) ** 0.5
ax.streamplot(x, y, u, v, transform=ccrs.PlateCarree(),linewidth=2, density=4,color=magnitude)

七、风向标

40.随机在x,y处生成风速(x、y为坐标,u、v为x方向风速和y方向风速)

代码语言:javascript复制
shape=(20, 30)

x = np.linspace(200, 391.1, shape[1])
y = np.linspace(-50, 50, shape[0])

x2d, y2d = np.meshgrid(x, y)
u = 10 * (2 * np.cos(2 * np.deg2rad(x2d)   3 * np.deg2rad(y2d   30)) ** 2)
v = 20 * np.cos(6 * np.deg2rad(x2d))

41.绘制地图底图

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([-90, 80, 10, 85], crs=ccrs.PlateCarree())
ax.stock_img()
ax.coastlines()

42.利用ax.barbs将第38题的风向标绘制在上一题的地图上

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([-90, 80, 10, 85], crs=ccrs.PlateCarree())
ax.stock_img()
ax.coastlines()

ax.barbs(x, y, u, v, length=5,
         sizes=dict(emptybarb=0.25, spacing=0.2, height=0.5),
         linewidth=0.95,alpha=0.5,transform=ccrs.PlateCarree())

八、夜影模式(0.17及以后版本的新功能)

43.利用ax.stock_img绘制默认地图背景

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)
ax = plt.axes(projection=ccrs.PlateCarree())

date = datetime.datetime(2022, 6, 4, 23)

ax.set_title(f'Night time shading for {date}')
ax.stock_img()

44.在上一题的基础上,添加夜影

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)
ax = plt.axes(projection=ccrs.PlateCarree())

date = datetime.datetime(2022, 6, 4, 23)

ax.set_title(f'Night time shading for {date}')
ax.stock_img()
ax.add_feature(Nightshade(date, alpha=0.2))

45.在当前目录下保存图片,并命名为Nightshade.png

代码语言:javascript复制
fig = plt.figure(figsize=(10, 5), dpi=500)
ax = plt.axes(projection=ccrs.PlateCarree())

date = datetime.datetime(2022, 6, 4, 23)

ax.set_title(f'Night time shading for {date}')
ax.stock_img()
ax.add_feature(Nightshade(date, alpha=0.2))

plt.savefig('Nightshade.png')

0 人点赞