带起点的流线图的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:效果很好:

Cartopy package

案例2:无起点的cartopy-Plate-Carree:工作正常:

Mpl-Plate-Carree without starting points

情况3:无起点的cartopy-Orthographic-0:工作正常(?),但高纬度被切断了:

Cartopy-Plate-Carree without starting points

案例4:无起点的cartopy-Orthographic-90:工作正常:

Cartopy-Orthographic-0

案例5:具有起点的Mpl-Plate-Carree:效果很好:

Cartopy-Orthographic-90 without starting points

案例6:带有起点的cartopy-Plate-Carree:未显示两个起点的两条流线:

Mpl-Plate-Carree with starting points

案例7:具有起点的cartopy-Orthographic-0:未显示任何数据(没有流线,起点等):

Cartopy-Plate-Carree with starting points

案例8:cartopy-Orthographic-90(具有起点):仅显示了北半球的一部分,并且流线看起来不正确:

enter image description here


摘要:案例6、7和8(cartopy Plate-Carree /正交0度倾斜/ Orthoographic 90度倾斜投影)给出了意外的,可能不正确的输出。有人可以向我解释为什么这些图看起来是这样吗?我是在误解我应该提供的输入内容吗?

小奖金问题:如果我想设置正交投影的轴限制(例如,0度倾斜度的纬度极限为[-90,+ 90]度,90度倾斜度的纬度极限为[0,+90]度,并且也包括经度限制),例如假设我只绘制30度以上的数据,这如何工作?我假设ax.set_xlimax.set_ylim不起作用(因为他们不知道投影)。如果需要通过

enter image description here

完成操作,我需要在其中设置哪些经度和纬度限制?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...