繁体   English   中英

Matplotlib Pyplot ImageGrid 图问题

[英]Matplotlib Pyplot ImageGrid Figure Problem

我遇到了 AXES_GRID1 的问题( https://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html )。 特别是在 ImageGrid() 的使用中。

以下代码准备并显示与预期完全相同的图形:

import matplotlib.pyplot as plt
# from mpl_toolkits.axes_grid1 import AxesGrid
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

fig = plt.figure(figsize=(15, 15))

grid = ImageGrid(fig, 111,
                nrows_ncols=(3, 4), 
                axes_pad=0.5,
                cbar_mode='single',
                cbar_location='right',
                cbar_pad=0.5,
                direction='row',
                )
plt.tight_layout()
plt.show()

要创建的图形概述

但是一旦我将我的数字添加到相关轴上,就像这样:

import matplotlib.pyplot as plt
# from mpl_toolkits.axes_grid1 import AxesGrid
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

fig = plt.figure(figsize=(15, 15))

grid = ImageGrid(fig, 111,
                nrows_ncols=(3, 4), 
                axes_pad=0.5,
                cbar_mode='single',
                cbar_location='right',
                cbar_pad=0.5,
                direction='row',
                )
rc = 0
for season in ('MAM', 'JJA', 'SON'):
    im = dat_fluo_ref.sel(time=select_season(dat_fluo_ref['time.season'], season)).groupby('time.hour').mean('time').radiance.plot.pcolormesh(
        ax=grid[rc + 0], vmin=0, vmax=0.5, cmap='Spectral_r', add_colorbar=False,)

    im = dat_fluo_inc.sel(time=select_season(dat_fluo_inc['time.season'], season)).groupby('time.hour').mean('time').radiance.plot.pcolormesh(
        ax=grid[rc + 1], vmin=0, vmax=0.5, cmap='Spectral_r', add_colorbar=False,)

    im = dat_full_ref.sel(time=select_season(dat_full_ref['time.season'], season)).groupby('time.hour').mean('time').radiance.plot.pcolormesh(
        ax=grid[rc + 2], vmin=0, vmax=0.5, cmap='Spectral_r', add_colorbar=False,)

    im = dat_full_inc.sel(time=select_season(dat_full_inc['time.season'], season)).groupby('time.hour').mean('time').radiance.plot.pcolormesh(
        ax=grid[rc + 3], vmin=0, vmax=0.5, cmap='Spectral_r', add_colorbar=False)
    rc += 4
cbar = grid.cbar_axes[0].colorbar(im)
plt.tight_layout()
plt.show()

然后生成的图形(参见2 )不再像预览图(参见1 )。

这可能是传递给 ImageGrid 的参数的问题。 图创建但完全错误的纵横比。看起来压缩了。

谢谢您的帮助!

ImageGrid正在尝试为像素保持正方形。 如果您的列多于行,那可能不是您想要完成的。 在构造 ImageGrid 时尝试传递aspect=False

相比:

from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure()
grid = ImageGrid(fig, 111,
                 nrows_ncols=(3, 4),
                 axes_pad=0.5,
                 cbar_mode='single',
                 cbar_location='right',
                 cbar_pad=0.5,
                 direction='row',
                 aspect=False
                )
for ax in grid.axes_all:
    ax.pcolormesh(np.random.random(size=(5,20)))

在此处输入图像描述

from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure()
grid = ImageGrid(fig, 111,
                 nrows_ncols=(3, 4),
                 axes_pad=0.5,
                 cbar_mode='single',
                 cbar_location='right',
                 cbar_pad=0.5,
                 direction='row',
                 aspect=True
                )
for ax in grid.axes_all:
    ax.pcolormesh(np.random.random(size=(5,20)))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM