“numpy.ndarray”对象没有属性“imshow”

omhiaaxx  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(214)

我一直在尝试一切我可以让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()

我敢肯定这与2D数组有关,但我真的想不出来。
我试

for i in ax:
    ax[i].imshow(img, interpolation='none')
# Show the figure on the screen
fig.show()

但我只是得到:
IndexError:只有整数、切片(:)、省略号(...)、numpy.newaxis(None)和整数或布尔数组才是有效的索引

fnatzsnv

fnatzsnv1#

这是:

for i in ax:
    ax[i].imshow(img, interpolation='none')

因为I不是索引。它是一个轴对象。
第一种情况是错误的,因为即使你循环了这些项,你调用的是ax上的函数,而不是单个轴。
执行以下操作:

for a in ax:
    a.imshow(img, interpolation='none')
pu3pd22g

pu3pd22g2#

只需在代码前的“ax.flatten()”之前添加此命令

ax = ax.flatten()
for a in ax:
    a.imshow(img, interpolation='none')
plt.show()
gupuwyp2

gupuwyp23#

  • 只是对以前的答案做了一点补充:*

变量axs,如果包含多个轴,将是一个2D ndarray。例如,可以使用以下命令创建3行2列的子图:

fig, axs = plt.subplots(ncols=2, nrows=3, figsize=(8, 10))

>> axs
array([[<AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>],
       [<AxesSubplot:>, <AxesSubplot:>]], dtype=object)

这个2D ndarray需要两个索引,要使它在循环中工作,需要一个索引。因此,它必须首先被展平才能有大小为(6,)的1D ndarray。

fig, axs = plt.subplots(ncols=2, nrows=3, figsize=(8, 10))
for i, ax in enumerate(axs.ravel()):
    ax.imshow(img[i])

或者,如果您想保持2D,也可以这样做

fig, axs = plt.subplots(ncols=2, nrows=3, figsize=(8, 10))
ax = axs.ravel()
for i in range(5):
    ax[i].imshow(img[i])
rkkpypqq

rkkpypqq4#

你可以像下面这样检查斧头

type(ax)
>>> <class 'numpy.ndarray'>

ax
>>> [<matplotlib.axes._subplots.AxesSubplot object at 0x0000028F13AFC668>
 <matplotlib.axes._subplots.AxesSubplot object at 0x0000028F15C6FCF8>
 <matplotlib.axes._subplots.AxesSubplot object at 0x0000028F15CA23C8>
 <matplotlib.axes._subplots.AxesSubplot object at 0x0000028F15CC9A58>
 <matplotlib.axes._subplots.AxesSubplot object at 0x0000028F15CFA160>]

如果你真想使用'i',那么就像这样使用enumerate()

for i, ax in enumerate(axs):
    ax.imshow(img[i:i*100], interpolation='none')

“axs”是首选的,因为它是多个。
最后,您可以在下面测试

import numpy as np
import matplotlib.pyplot as plt
from skimage import data

'''Read the image data'''
img = data.chelsea()   # cat image

'''Show the image data'''
# Create figure with 1 subplot
fig, axs = plt.subplots(nrows=1, ncols=5, figsize=(10, 3))

print(axs)
# [<matplotlib.axes._subplots.AxesSubplot object at 0x000001D7A841C710>
#  <matplotlib.axes._subplots.AxesSubplot object at 0x000001D7AA58FCC0>
#  <matplotlib.axes._subplots.AxesSubplot object at 0x000001D7AA5C2390>
#  <matplotlib.axes._subplots.AxesSubplot object at 0x000001D7AA5E9A20>
#  <matplotlib.axes._subplots.AxesSubplot object at 0x000001D7AA61A128>]

print(axs.shape)  # (5,)

# Show the image data in a subplot
for i, ax in enumerate(axs):
    print(ax)     # AxesSubplot(0.125,0.11;0.133621x0.77)
    img_made_changeable = img[i:(i + 2) * 50]
    ax.imshow(img_made_changeable, interpolation='none')

# Show the figure on the screen
plt.show()

相关问题