17

I'm relatively new to all this and I started the tutorial on image analysis here. When trying to perform the pylab.imshow(dna) step it returns the following error:

In [10]: pylab.imshow(dna)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-fc86cadb4e46> in <module>()
----> 1 pylab.imshow(dna)

 /usr/lib/pymodules/python2.7/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect,    interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2375         ax.hold(hold)
   2376     try:
-> 2377         ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   2378         draw_if_interactive()
   2379     finally:

/usr/lib/pymodules/python2.7/matplotlib/axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   6794                        filterrad=filterrad, resample=resample, **kwargs)
   6795 
-> 6796         im.set_data(X)
   6797         im.set_alpha(alpha)
   6798         self._set_artist_props(im)

/usr/lib/pymodules/python2.7/matplotlib/image.pyc in set_data(self, A)
    409         if (self._A.ndim not in (2, 3) or
    410             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 411             raise TypeError("Invalid dimensions for image data")
    412 
    413         self._imcache =None

TypeError: Invalid dimensions for image data

Fairly certain I have followed all the instructions in the tutorial to the letter but I can't work out was is going wrong.

iacob
  • 20,084
  • 6
  • 92
  • 119
Samuel Barnett
  • 434
  • 1
  • 3
  • 13
  • 1
    what is `dna`? (what does `type(dna)` and `dna.shape` give?) It is raising `TypeError` because it is not a type or shape that `imshow` knows how to deal with. – tacaswell Feb 21 '13 at 17:04
  • 1
    it's just what the image is saved as in `dna = mahotas.imread('dna.jpeg')` `type(dna)` gives numpy.ndarray and `dna.shape` gives (1024, 1344, 1) – Samuel Barnett Feb 21 '13 at 17:28

3 Answers3

43

it's just what the image is saved as in dna = mahotas.imread('dna.jpeg') type(dna) gives numpy.ndarray and dna.shape gives (1024, 1344, 1)

This is the problem, if you pass a 3D ndarray, it expects that you will have 3 or 4 planes (RGB or RGBA) (Read the code on line 410 in the last frame of the stack trace).

You just need to get rid of the extra dimension using

dna = dna.squeeze()

or

imshow(dna.squeeze())

To see what squeeze is doing, see the following example:

a = np.arange(25).reshape(5, 5, 1)
print a.shape # (5, 5, 1)
b = a.squeeze()
print b.shape # (5, 5)
iacob
  • 20,084
  • 6
  • 92
  • 119
tacaswell
  • 84,579
  • 22
  • 210
  • 199
0

As of v3.3 this error is no longer raised, as 3d arrays of size MxNx1 are now coerced into MxN for displaying.

iacob
  • 20,084
  • 6
  • 92
  • 119
0
import matplotlib.pyplot as plt

plt.imshow(img.reshape(48, 48)) # for example: 48
Muhteva
  • 2,729
  • 2
  • 9
  • 21
  • Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. That's especially important when there's already an accepted answer that's been validated by the community. Under what conditions might your approach be preferred? Are you taking advantage of new capabilities? – Jeremy Caney Aug 25 '21 at 08:34