【问题标题】:why it is showing " TypeError: Invalid shape (20,) for image data "为什么它显示“TypeError: Invalid shape (20,) for image data”
【发布时间】:2020-04-26 11:02:32
【问题描述】:
    import numpy as np
    import matplotlib.pyplot as plt  
    m1=np.random.randint(0,20,4*5)
    m1.reshape(4,5)
    plt.imshow(m1)

**当我在 jupyter notebook 上的 python 3 中执行上述代码时,出现类型错误任何人请以最简单的方式回答此错误** 像 TypeError Traceback(最近一次调用最后一次) 在 ----> 1 plt.imshow(m1)

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, data, **kwargs)
   2649         filternorm=filternorm, filterrad=filterrad, imlim=imlim,
   2650         resample=resample, url=url, **({"data": data} if data is not
-> 2651         None else {}), **kwargs)
   2652     sci(__ret)
   2653     return __ret

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1563     def inner(ax, *args, data=None, **kwargs):
   1564         if data is None:
-> 1565             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1566 
   1567         bound = new_sig.bind(ax, *args, **kwargs)

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    356                 f"%(removal)s.  If any parameter follows {name!r}, they "
    357                 f"should be pass as keyword, not positionally.")
--> 358         return func(*args, **kwargs)
    359 
    360     return wrapper

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs)
    356                 f"%(removal)s.  If any parameter follows {name!r}, they "
    357                 f"should be pass as keyword, not positionally.")
--> 358         return func(*args, **kwargs)
    359 
    360     return wrapper

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5613                               resample=resample, **kwargs)
   5614 
-> 5615         im.set_data(X)
   5616         im.set_alpha(alpha)
   5617         if im.get_clip_path() is None:

c:\users\jaiprakash\appdata\local\programs\python\python37-32\lib\site-packages\matplotlib\image.py in set_data(self, A)
    697                 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
    698             raise TypeError("Invalid shape {} for image data"
--> 699                             .format(self._A.shape))
    700 
    701         if self._A.ndim == 3:

TypeError: Invalid shape (20,) for image data

【问题讨论】:

标签: numpy matplotlib random python-3.7 imshow


【解决方案1】:

当您调用m1.reshape(4,5) 时,您不会将其分配给变量。该方法不会改变m1 的形状,除非您将其重新分配给m1

import numpy as np
import matplotlib.pyplot as plt

#m1=np.random.randint(low=0, high=20, size=(4,5)) # << personally I would have done this & not bothered with the reshape

m1 = np.random.randint(0,20,4*5)
m1 = m1.reshape(4, 5)
plt.imshow(m1)

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多