简体   繁体   中英

numpy.ndarray object has no attribute imshow

I am trying to use python with jupyter notebook to come up with a scheme where you can hold on to a smart lightbulb and get information from the hand by diffuse reflectance. I've managed to make all of that work, and all of this is saved in a variable PIX:

PIX = np.array(pictures)
print(PIX.shape)

And this outputs an (81,480,640,3) just as expected (the 81 represents the visible spectrum converted to RGB so that the lamp gets it).

However, I now want to visualize the data, and I thought that imshow was the perfect implementation. I looked around and Datacamp had a nice summary of this in action and copied and pasted from https://campus.datacamp.com/courses/biomedical-image-analysis-in-python/exploration?ex=11 . I changed some of the variables so that the script looks like the following:

# Plot the images on a subplots array 
fig, axes = 
plt.subplots(int(PIX.shape[0]/9),int(PIX.shape[0]/9))

for i, ax in enumerate(axes):
    axes[i].imshow(PIX[i,:,:,0], interpolation='none')
# Render the figure
plt.show()

Again, this is fairly simple. However, I get the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-20-a7bb604d1828> in <module>
      3 
      4 for i, ax in enumerate(axes):
----> 5     axes[i].imshow(PIX[i,:,:,0], interpolation='none')
      6 # Render the figure
      7 plt.show()

AttributeError: 'numpy.ndarray' object has no attribute 'imshow' 

I tried the fixes on 'numpy.ndarray' object has no attribute 'imshow' and 'numpy.ndarray' object has no attribute 'show' using matplotlib , who seemed to have similar problems. However, none of the fixes seem to work in my case.

Any help would be appreciated!

axes is a 2D ndarray so you have to use two indices.

Alternatively, you could replace

for i, ax in enumerate(axes)

With

for i, ax in enumerate(axes.ravel())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM