【发布时间】:2021-02-27 04:24:57
【问题描述】:
我的数据集由 50 个大小为 8 x 8 的图像组成,这些图像被展平为大小为 64 的数组。使用 matplotlib.pyplot (plt) 的函数 imshow 可视化前 16 个图像(在 4x4 网格中)数据集。
我尝试了以下代码:
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(50,64) #this will simulate my data
fig=plt.figure(figsize=(8, 8))
for i in range(16):
img = np.reshape(data[i:(i+1)],(8,8))
fig.add_subplot(4, 4, i)
plt.imshow(img)
plt.show()
这是回溯:
<ipython-input-207-7af8d2013358> in plot_first_digits()
13
14 img = np.reshape(X[i:(i+1)],(8,8))
---> 15 fig.add_subplot(4, 4, i)
16 plt.imshow(img)
17
~/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py in add_subplot(self, *args, **kwargs)
1417 self._axstack.remove(ax)
1418
-> 1419 a = subplot_class_factory(projection_class)(self, *args, **kwargs)
1420
1421 return self._add_axes_internal(key, a)
~/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_subplots.py in __init__(self, fig, *args, **kwargs)
63 else:
64 if num < 1 or num > rows*cols:
---> 65 raise ValueError(
66 f"num must be 1 <= num <= {rows*cols}, not {num}")
67 self._subplotspec = GridSpec(
ValueError: num must be 1 <= num <= 16, not 0
<Figure size 576x576 with 0 Axes>
【问题讨论】:
-
你能分享回溯吗?
-
什么是回溯?
-
运行此脚本时 Python 返回的错误
标签: python image numpy matplotlib image-processing