Python 带起点的流图的Cartopy platecarree和正交投影问题
我想使用Python 带起点的流图的Cartopy platecarree和正交投影问题,python,matplotlib,cartopy,map-projections,Python,Matplotlib,Cartopy,Map Projections,我想使用matplotlib函数,将速度场(u,v)绘制为(经度,纬度)的函数,并将其作为流图、板内卡利(lon-lat)和正交投影。我希望固定流图的起点,因为我希望最终创建一些特征在球体上旋转的动画,其中流线“锚定”在某处。然而,我在这些预测中遇到了一些问题,下面将进一步详述 我使用以下软件包版本:Python3.7.6、matplotlib3.2.2、cartopy0.18.0 创建包含或不包含上述预测起点的流线图的“最小”示例为: import numpy as np import ma
matplotlib
函数,将速度场(u,v)绘制为(经度,纬度)的函数,并将其作为流图、板内卡利(lon-lat)和正交投影。我希望固定流图的起点,因为我希望最终创建一些特征在球体上旋转的动画,其中流线“锚定”在某处。然而,我在这些预测中遇到了一些问题,下面将进一步详述
我使用以下软件包版本:Python
3.7.6、matplotlib
3.2.2、cartopy
0.18.0
创建包含或不包含上述预测起点的流线图的“最小”示例为:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# define longitude and latitude grids
nlon, nlat = 48, 24
dlon, dlat = 7.5, 7.5
lons = np.linspace(-180+dlon,180,nlon)
lats = np.linspace(-90,90-dlat,nlat)
# define random velocity fields
np.random.seed(999)
u = np.random.rand(nlat, nlon)
v = np.random.rand(nlat, nlon)
# get indices of latitudes north of the equator and longitudes within +/- 90 deg
ilat_north = np.where(lats >= 0)[0]
ilon = np.where(np.abs(lons) <= 90)[0]
# create a list of 5 starting points at various latitudes aligned at a longitude of 0 deg
start_points = list(zip(list(np.linspace(0,0,5)),list(np.linspace(0,80,5))))
# output plot directory and whether plots should be saved
plot_dir = '/home/proxauf/test_streamplot_cartopy'
save_plots = True
def plot_streamplot(mode, start_points=None, figtitle=None, outname=None, save_plot=False):
fig = plt.figure()
if mode == 'mpl-platecarree':
# a Plate-Carree projection without Cartopy (directly using matplotlib)
ax = plt.gca()
ax.streamplot(lons, lats[ilat_north], u[ilat_north,:], v[ilat_north,:], start_points=start_points)
elif 'cartopy' in mode:
if mode == 'cartopy-platecarree':
# a Plate-Carree projection with Cartopy
ax = plt.subplot(projection=ccrs.PlateCarree())
ax.streamplot(lons, lats[ilat_north], u[ilat_north,:], v[ilat_north,:], start_points=start_points, transform=ccrs.PlateCarree())
elif mode == 'cartopy-orthographic0':
# an orthographic projection with a latitudinal inclination of 0 deg with Cartopy
# restrict longitudes to +/- 90 deg, otherwise an error occurs (outside of projection area)
ax = plt.subplot(projection=ccrs.Orthographic(central_longitude=0,central_latitude=0))
ax.streamplot(lons[ilon], lats[ilat_north], u[ilat_north[:,None],ilon[None,:]], v[ilat_north[:,None], ilon[None,:]], start_points=start_points, transform=ccrs.PlateCarree())
if mode == 'cartopy-orthographic90':
# an orthographic projection with a latitudinal inclination of 90 deg with Cartopy
ax = plt.subplot(projection=ccrs.Orthographic(central_longitude=0,central_latitude=90))
ax.streamplot(lons, lats[ilat_north], u[ilat_north,:], v[ilat_north,:], start_points=start_points, transform=ccrs.PlateCarree())
if 'platecarree' in mode:
ax.set_xlim(-180,180)
ax.set_ylim(-90,90)
ax.set_aspect('equal')
if 'cartopy' in mode:
# draw gridlines with label for visual orientation
ax.gridlines(draw_labels=True)
if start_points is not None:
# plot starting points for the streamplot
for i in start_points:
if 'cartopy' in mode:
ax.plot(i[0],i[1],marker='o',transform=ccrs.PlateCarree())
else:
ax.plot(i[0],i[1],marker='o')
fig.suptitle(figtitle)
if save_plot:
# save the plot and close it
plt.savefig('%s/%s' % (plot_dir, outname))
plt.close()
# create and save streamplots for different projections, with and without starting points
plot_streamplot(mode='mpl-platecarree', start_points=None, figtitle='mpl-platecarree, without start points', outname='test_streamplot_mpl_pc_sp_no.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-platecarree', start_points=None, figtitle='cartopy-platecarree, without start points', outname='test_streamplot_cartopy_pc_sp_no.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic0', start_points=None, figtitle='cartopy-orthographic0, without start points', outname='test_streamplot_cartopy_ortho0_sp_no.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic90', start_points=None, figtitle='cartopy-orthographic90, without start points', outname='test_streamplot_cartopy_ortho90_sp_no.png', save_plot=save_plots)
plot_streamplot(mode='mpl-platecarree', start_points=start_points, figtitle='mpl-platecarree, with start points', outname='test_streamplot_mpl_pc_sp_yes.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-platecarree', start_points=start_points, figtitle='cartopy-platecarree, with start points', outname='test_streamplot_cartopy_pc_sp_yes.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic0', start_points=start_points, figtitle='cartopy-orthographic0, with start points', outname='test_streamplot_cartopy_ortho0_sp_yes.png', save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic90', start_points=start_points, figtitle='cartopy-orthographic90, with start points', outname='test_streamplot_cartopy_ortho90_sp_yes.png', save_plot=save_plots)
将numpy导入为np
将matplotlib.pyplot作为plt导入
将cartopy.crs作为CCR导入
#定义经纬度栅格
nlon,nlat=48,24
dlon,dlat=7.5,7.5
lons=np.linspace(-180+dlon,180,nlon)
lats=np.linspace(-90,90-dlat,nlat)
#定义随机速度场
np.随机种子(999)
u=np.random.rand(nlat,nlon)
v=np.random.rand(nlat,nlon)
#获取赤道以北的纬度指数和+/-90度范围内的经度指数
ilat_north=np.其中(纬度>=0)[0]
ilon=np.where(np.abs(lons)