如何解决带起点的流线图的Cartopy Platecarree和正射投影问题
我想使用matplotlib
函数{在板斜角(lon-lat)和正投影中绘制速度场(u,v)作为(经度,纬度)作为流图的函数。 {3}}和plt.streamplot。我希望固定流图的起点,因为我最终希望创建一些动画,这些动画在球体上旋转,并且流线“固定”在某处。但是,我在这些预测上遇到了许多问题,下面将对此进行详细介绍。
我使用以下软件包版本: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,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,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],u[ilat_north[:,None],ilon[None,:]],v[ilat_north[:,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,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],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',figtitle='mpl-platecarree,without start points',outname='test_streamplot_mpl_pc_sp_no.png',save_plot=save_plots)
plot_streamplot(mode='cartopy-platecarree',figtitle='cartopy-platecarree,outname='test_streamplot_cartopy_pc_sp_no.png',save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic0',figtitle='cartopy-orthographic0,outname='test_streamplot_cartopy_ortho0_sp_no.png',save_plot=save_plots)
plot_streamplot(mode='cartopy-orthographic90',figtitle='cartopy-orthographic90,outname='test_streamplot_cartopy_ortho90_sp_no.png',save_plot=save_plots)
plot_streamplot(mode='mpl-platecarree',with start points',outname='test_streamplot_mpl_pc_sp_yes.png',outname='test_streamplot_cartopy_pc_sp_yes.png',outname='test_streamplot_cartopy_ortho0_sp_yes.png',outname='test_streamplot_cartopy_ortho90_sp_yes.png',save_plot=save_plots)
下面是这个最小示例创建的输出。
案例1:没有起点的Mpl-Plate-Carree:效果很好:
案例2:无起点的cartopy-Plate-Carree:工作正常:
情况3:无起点的cartopy-Orthographic-0:工作正常(?),但高纬度被切断了:
案例4:无起点的cartopy-Orthographic-90:工作正常:
案例5:具有起点的Mpl-Plate-Carree:效果很好:
案例6:带有起点的cartopy-Plate-Carree:未显示两个起点的两条流线:
案例7:具有起点的cartopy-Orthographic-0:未显示任何数据(没有流线,起点等):
案例8:cartopy-Orthographic-90(具有起点):仅显示了北半球的一部分,并且流线看起来不正确:
摘要:案例6、7和8(cartopy Plate-Carree /正交0度倾斜/ Orthoographic 90度倾斜投影)给出了意外的,可能不正确的输出。有人可以向我解释为什么这些图看起来是这样吗?我是在误解我应该提供的输入内容吗?
小奖金问题:如果我想设置正交投影的轴限制(例如,0度倾斜度的纬度极限为[-90,+ 90]度,90度倾斜度的纬度极限为[0,+90]度,并且也包括经度限制),例如假设我只绘制30度以上的数据,这如何工作?我假设ax.set_xlim
或ax.set_ylim
不起作用(因为他们不知道投影)。如果需要通过完成操作,我需要在其中设置哪些经度和纬度限制?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。