matplotlib子图-数组的索引过多 [英] matplotlib subplots - too many indices for array
本文介绍了matplotlib子图-数组的索引过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对plt.subplots的工作方式很困惑
I'm quite confused with the way plt.subplots work
此代码段有效 - 显示 2 x 2 布局
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)
此代码段不起作用 - 1 x 2 布局
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)
此错误的出现是 IndexError:数组的索引过多
当我打印 axs 形状时,在第一种情况下为 (2, 2)
而在第二种情况下为 (2,)
.这是什么斧头?我如何使第二段代码工作?
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?
推荐答案
你的第二个图本质上是一个一维数组.试试没有第二个坐标的代码.
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)
这篇关于matplotlib子图-数组的索引过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文