【发布时间】:2021-05-23 07:33:14
【问题描述】:
我正在使用 mpl_toolkits.axes_grid1.Imagegrid 绘制具有 4*6 网格的图像:
target = np.reshape(target, (12, 41, 81))
output = np.reshape(output, (12, 41, 81))
pred_error = target - output
# target: C x D x H x W
sfmt = ticker.ScalarFormatter(useMathText=True)
sfmt.set_powerlimits((-2, 2))
cmap = 'jet'
fig = plt.figure(1, (11, 5.5))
axes_pad = 0.1
cbar_pad = 0.1
label_size = 6
plt.rcParams["mpl_toolkits.legacy_colorbar"] = False
subplots_position = [(4,6,i) for i in range(1, 25)]
for i, subplot_i in enumerate(subplots_position):
if i in [i for i in range(6)]+[i for i in range(12, 18)]:
# share one colorbar
grid = ImageGrid(fig, subplot_i, # as in plt.subplot(111)
nrows_ncols=(2, 1),
axes_pad=axes_pad,
share_all=False,
cbar_location="right",
cbar_mode="single",
cbar_size="3%",
cbar_pad=cbar_pad,
)
if i <6:
data = (target[i], output[i])
else:
data = (target[i-6], output[i-6])
channel = np.concatenate(data)
vmin, vmax = np.amin(channel), np.amax(channel)
# Add data to image grid
for j, ax in enumerate(grid):
im = ax.imshow(data[j], vmin=vmin, vmax=vmax, cmap=cmap)
ax.set_axis_off()
# ticks=np.linspace(vmin, vmax, 10)
#set_ticks, set_ticklabels
cbar = grid.cbar_axes[0].colorbar(im, format=sfmt)
# cbar.ax.set_yticks((vmin, vmax))
cbar.ax.yaxis.set_offset_position('left')
cbar.ax.tick_params(labelsize=label_size)
cbar.ax.toggle_label(True)
else:
grid = ImageGrid(fig, subplot_i, # as in plt.subplot(111)
nrows_ncols=(1, 1),
axes_pad=axes_pad,
# share_all=True,
# aspect=True,
cbar_location="right",
cbar_mode="single",
cbar_size="6%",
cbar_pad=cbar_pad,
)
data = [pred_error[i%12]]
for j, ax in enumerate(grid):
im = ax.imshow(data[j], cmap=cmap)
ax.set_axis_off()
ax.set_axes_locator
cbar = grid.cbar_axes[j].colorbar(im, format=sfmt)
grid.cbar_axes[j].tick_params(labelsize=label_size)
grid.cbar_axes[j].toggle_label(True)
plt.tight_layout()##pad=0.25, w_pad=0.25, h_pad=0.25)
# fig.subplots_adjust(wspace=0.075, hspace=0.075)
plt.show()
plt.savefig('test.pdf',
dpi=300, bbox_inches='tight')
plt.show()
plt.close(fig)
target 为 (2,6,41,81) 形状数组,绘图时调整大小为 (12,41,81),输出 维度相同, pred_error 是它们之间的区别。我想在第 1 行和第 4 行显示 target,在第 2 行和第 5 行显示 output,使用相同的颜色条,在第 3 和第 6 行显示 pred_error .
我知道问题在于我图像中的网格大小都相同,但我没有找到编辑这些绿色图像网格高度的方法。感谢您的帮助!!!
【问题讨论】:
-
Imagegrid 的设计使得网格默认大小相同,更改它不会太容易,我没有找到方便的方法。
标签: python matplotlib subplot colorbar