Matplotlib 子图 - 数组索引太多



我对 plt.subplots 的工作方式很困惑

此代码段有效 - 显示 2 x 2 布局

fig, axs = plt.subplots(2,2, figsize=(20, 10))
axs[0,0].set_title('Sobel')
axs[0,0].imshow(sobelx)
axs[0,1].set_title('S Channel')
axs[0,1].imshow(s_channel)
axs[1,0].set_title('Combined Binary')
axs[1,0].imshow(combined_binary)
axs[1,1].set_title('Color Stack')
axs[1,1].imshow(color_stack)

此代码段不起作用 - 1 x 2 布局

fig, axs = plt.subplots(1,2, figsize=(20, 10))
axs[0,0].set_title('Undistorted Image')
axs[0,0].imshow(undistort_img)
axs[0,1].set_title('Warped Image')
axs[0,1].imshow(warped_img)

这会产生错误IndexError: too many indices for array

当我打印 axs 形状时,在第一种情况下(2, 2),而在第二种情况下(2,)。这是什么斧头?我如何使第二段代码工作?

你的第二个图本质上是一个一维数组。尝试不使用第二个坐标的代码。

fig, axs = plt.subplots(1,2, figsize=(20, 10))
axs[0].set_title('Undistorted Image')
axs[0].imshow(undistort_img)
axs[1].set_title('Warped Image')
axs[1].imshow(warped_img)

最新更新