从两个xarray并排绘制两个图形

问题描述 投票:0回答:1

我正在尝试并排绘制两个子图。每个子图都是xarray的变量,而图是投影。

我的绘图功能是:

def plot_avg(data, **kwargs):


    ax = plt.axes(projection=ccrs.Orthographic(central_latitude=-90.0))

    ax.coastlines(resolution='10m',zorder=3)
    ax.gridlines(color='gray')
    ax.add_feature(cartopy.feature.LAND, zorder=1,facecolor=cartopy.feature.COLORS['land_alt1'])
    data.plot(ax = ax, transform=ccrs.PlateCarree(),
               vmin = 34.4 , vmax=35.81 , levels=17 ,
               cmap=Becki, cbar_kwargs={'shrink': 0.3})

并且主要代码是:

fig = plt.figure()

ax1 = fig.add_subplot(111)
plot_avg(var1)
ax2 = fig.add_subplot(122)
plot_avg(var2)

但是由于某些原因我无法做到,我总是得到一个带有两个颜色条的图:

enter image description here

我可以做些什么?我很难处理来自xarray的图对象。

用于绘图的样本数据文件在这里:netcdf files

python matplotlib python-xarray cartopy
1个回答
0
投票

尝试将ax定义为函数的参数:

def plot_avg(data, ax, **kwargs):


    ax = plt.axes(projection=ccrs.Orthographic(central_latitude=-90.0))

    ax.coastlines(resolution='10m',zorder=3)
    ax.gridlines(color='gray')
    ax.add_feature(cartopy.feature.LAND, zorder=1,facecolor=cartopy.feature.COLORS['land_alt1'])
    data.plot(ax = ax, transform=ccrs.PlateCarree(),
               vmin = 34.4 , vmax=35.81 , levels=17 ,
               cmap=Becki, cbar_kwargs={'shrink': 0.3})

然后将您创建的ax传递到函数调用中:

fig = plt.figure()

ax1 = fig.add_subplot(111)
plot_avg(var1, ax1)
ax2 = fig.add_subplot(122)
plot_avg(var2, ax2)
© www.soinside.com 2019 - 2024. All rights reserved.