[英]Matplotlib Pyplot ImageGrid Figure Problem
I'm stuck at a problem with AXES_GRID1 ( https://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html ).我遇到了 AXES_GRID1 的问题( https://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html )。 Specifically in the use of ImageGrid().
特别是在 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 ).然后生成的图形(参见2 )不再像预览图(参见1 )。
It is probably an issue with the parameters passed to ImageGrid.这可能是传递给 ImageGrid 的参数的问题。
Thank you for your help!谢谢您的帮助!
ImageGrid
is attempting to maintain a square shape for pixels. ImageGrid
正在尝试为像素保持正方形。 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.在构造 ImageGrid 时尝试传递
aspect=False
。
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)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.