我一直在尽我所能让 pyplot 显示图像 5 次。我不断收到此错误…
这是我的代码
import matplotlib.pyplot as plt
import os.path
import numpy as np
'''Read the image data'''
# Get the directory of this python script
directory = os.path.dirname(os.path.abspath(__file__))
# Build an absolute filename from directory + filename
filename = os.path.join(directory, 'cat.gif')
# Read the image data into an array
img = plt.imread(filename)
'''Show the image data'''
# Create figure with 1 subplot
fig, ax = plt.subplots(1, 5)
# Show the image data in a subplot
for i in ax:
ax.imshow(img, interpolation='none')
# Show the figure on the screen
fig.show()
我确定它与二维数组有关,但我真的想不通。
我试过了
for i in ax:
ax[i].imshow(img, interpolation='none')
# Show the figure on the screen
fig.show()
但我只是得到:
IndexError:只有整数、切片(
:
)、省略号(...
)、numpy.newaxis(None
)和索引是有效的整数数组原文由 Reed Johnson 发布,翻译遵循 CC BY-SA 4.0 许可协议
这个:
没有意义,因为
I
不是索引。它是轴对象之一。你的第一个案例是错误的,因为即使你遍历了这些项目,你还是在
ax
上调用了函数,而不是在各个轴上。做这个: