I have dataset which consists of 50 images of size 8 x 8 flattened into arrays of size 64. Using the function imshow of matplotlib.pyplot (plt) to visualize the first 16 images (in a 4x4 grid) of the dataset.
I tried the following code:
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()
this is the traceback:
<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>

