Visualize the MNIST dataset with Matplotlib or OpenCV
Codemia Knowledge Hub ·
Use matplotlib.pyplot.imshow() with cmap='gray' for quick plots, or OpenCV's cv2.imshow() for interactive windows. MNIST has 70,000 grayscale images of handwritten digits, each 28x28 pixels. Matplotlib is better for grids, annotations and notebooks; OpenCV is better for real-time image processing pipelines. With either one, images loaded as flat vectors (784,) must be reshaped to 2D arrays (28, 28) first.
Loading MNIST
A single image with Matplotlib
Always pass cmap='gray' for grayscale data. Without it, Matplotlib applies a color map (viridis by default) and the digits appear blue and yellow instead of white and black.
A grid of images
One example per digit
Displaying with OpenCV
Two things trip people up with OpenCV:
- The window does not appear or freezes:
cv2.imshow()needscv2.waitKey()to process window events. Usecv2.waitKey(0)to wait for a keypress orcv2.waitKey(1)in a loop for real-time display. - Pixel values in the wrong range: OpenCV
imshowexpects uint8 (0-255) for grayscale display. If your images are normalized to 0.0-1.0 (common after preprocessing), multiply by 255 and cast with(img * 255).astype(np.uint8), or the display appears all black or all white.
Pixel value distribution and average digits
Reshaping flat vectors
Some sources (scikit-learn's fetch_openml, CSV files) return MNIST as flat 784-element vectors. Passing one to imshow() fails with a shape error, so reshape first:
TensorFlow vs PyTorch image formats
TensorFlow MNIST returns (batch, 28, 28) with values 0-255. PyTorch transforms often produce (batch, 1, 28, 28) with values 0.0-1.0. Before visualizing PyTorch tensors, squeeze the channel dimension and convert with img.squeeze().numpy(). In general, use 0-255 for OpenCV; Matplotlib accepts 0-1 or 0-255.
Related reading
- What algorithm could be used to identify if images are the same or similar, regardless of size?
- What algorithm does Photoshop use to desaturate an image?
- What algorithm to use to segment a sequence of numbers into n subsets, to minimize the standard deviation of the sum of the numbers in each subset
- What are good features for classifying photos of clothing?
- Visualizing attention activation in Tensorflow
- Visualizing branch topology in Git
- What are possible values for data_augmentation_options in the TensorFlow Object Detection pipeline configuration?
- What are the uses of tf.space_to_depth?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.