我想用matplotlib
函数plt.streamplot和Cartopy package绘制一个速度场(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)
下面是这个最小示例创建的输出
案例1:无起点的Mpl板材搬运:工作正常:
案例2:无起点的Cartopy板卡利:工作正常:
案例3:Cartopy-Orthographic-0无起点:工作正常(?),但高纬度被切断:
案例4:Cartopy-Orthographic-90无起点:工作正常:
案例5:Mpl板材搬运,起点:工作良好:
案例6:带起点的Cartopy板卡雷:未显示两个起点的两条流线:
案例7:带起点的Cartopy-Orthographic-0:未显示任何数据(无流线、起点等):
案例8:Cartopy-Orthographic-90,起点:仅显示北半球的一部分,流线似乎不正确:
总结:案例6、7和8(Cartopy Plate Carree/Orthographic-0deg-倾角/Orthographic-90deg-倾角投影)给出了意外的、可能不正确的输出。有人能给我解释一下为什么情节是这样的吗?我是否误解了我应该给出的输入?
小问题:如果我想为正交投影设置轴限制(例如,0度倾角的纬度限制为[-90,+90]度,90度倾角的纬度限制为[0,+90]度,还有经度限制),例如,假设我只打印30度以上的数据,这是如何工作的ax.set_xlim
或ax.set_ylim
不起作用,我假设(因为他们不知道投影)。如果这需要通过ax.set_extent完成,我需要在那里设置哪些经度和纬度限制
目前没有回答
相关问题 更多 >
编程相关推荐