16

I'm quite confused with the way plt.subplots work

This snippet works - displays a 2 by 2 Layout

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)

This snippet doesn't work - 1 by 2 Layout

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)

This errors out with IndexError: too many indices for array

When I print axs shape, it is (2, 2) in the first case where as (2,) in the second case. What is this axs ? And how do i make the 2nd piece of code work?

Ravi
  • 561
  • 2
  • 7
  • 19

1 Answers1

36

Your second plot is essentially a one-dimensional array. Try the code without the second coordinates.

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)
kkcheng
  • 402
  • 4
  • 8