简体   繁体   中英

Matplotlib Pyplot ImageGrid Figure Problem

I'm stuck at a problem with AXES_GRID1 ( https://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html ). Specifically in the use of ImageGrid().

The following code prepares and shows a figure exactly like intended:

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()

要创建的图形概述

But as soon as I add my figures to the relevant axes like so:

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()

Then the resulting figure (see 2 ) is no longer looking like the preview (see 1 ).

It is probably an issue with the parameters passed to ImageGrid. 图创建但完全错误的纵横比。看起来压缩了。

Thank you for your help!

ImageGrid is attempting to maintain a square shape for pixels. If you have many more columns than lines, that may not be what you are trying to accomplish. Try passing aspect=False when constructing the ImageGrid.

Compare:

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)))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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