numpy - 为什么它显示“ TypeError: Invalid shape (20,) for image data”
问题描述
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 中执行上述代码时,我收到类型错误任何人请以最简单的方式回答此错误 ** 就像 ----> 1 plt.imshow(米1)
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
解决方案
当您打电话时,您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)
推荐阅读
- flutter - 如何禁用 Flutter 中的任何小部件?
- rpm - RPM 规范决定首先安装哪个依赖项
- c# - 从 c# winforms 中的文件加载富文本框
- reactjs - ReactJs:在“span”标签 ES6 中监听事件
- wordpress - 除非普通/默认,否则 Wordpress 永久链接重定向到主页
- elasticsearch - 在 ElasticSearch 中使用单个查询进行多个搜索
- javascript - 如何根据本机反应中的值更改BackgroundColor
- scenekit - 移动一个 SCNNode 并使其与移动前的相同点
- excel - For循环中if语句的问题
- python - 如果对等公钥未编码为主机私钥,如何生成 DH 共享密钥?