Display MNIST image using matplotlib
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Displaying an MNIST digit with matplotlib is simple once the array shape is correct. MNIST images are grayscale 28 x 28 arrays, so the main job is to load one sample, make sure it is two-dimensional, and render it with a grayscale color map.
Load MNIST
A convenient source is the Keras dataset loader.
For the built-in loader, each image is already shaped as 28 x 28, which means it is ready for plotting.
Display One Image
cmap="gray" matters because MNIST is a single-channel grayscale dataset. Without it, matplotlib may choose an arbitrary default color map.
Display Several Digits in a Grid
A grid is useful for sanity checking labels or preprocessing.
This gives you a quick visual check that the data looks like handwritten digits rather than scrambled arrays.
If the Data Is Flattened
Some preprocessing pipelines flatten each image into a length-784 vector. In that case, reshape before plotting.
This reshape step is one of the most common missing pieces when people say the digit does not display correctly.
Useful Display Tweaks
For small images like MNIST, interpolation="nearest" can make the pixels easier to inspect.
This is handy when you are debugging thresholding, normalization, or image centering.
A very practical debugging trick is to display the true label and a model prediction in the title after training. That helps you inspect misclassified digits quickly and notice patterns such as poor centering, heavy normalization artifacts, or classes the model often confuses. Visualization is not just for demos; it is one of the fastest ways to sanity-check a classification pipeline.
If your pipeline normalizes pixel values into the range from 0.0 to 1.0, matplotlib still displays them correctly as long as the array shape is right. Problems that look like rendering errors are often really preprocessing errors such as incorrect reshape order, inverted values, or accidental batching dimensions left on the sample.
Displaying several digits side by side is also a quick sanity check for shuffled labels and dataset corruption.
It is also useful for comparing raw input images with normalized or augmented versions during debugging.
Common Pitfalls
- Forgetting to reshape flattened vectors back to
28 x 28. - Omitting
cmap="gray"and getting a misleading colorized image. - Passing arrays with extra batch or channel dimensions when you only want to display one sample.
- Confusing normalized pixel values with broken image rendering.
- Forgetting
plt.show()in environments where rendering is not automatic.
Summary
- MNIST digits should be displayed as
28 x 28grayscale images. - '
plt.imshow(image, cmap="gray")is the standard way to render one sample.' - Reshape flattened vectors of length
784before plotting. - Use subplot grids to inspect multiple digits quickly.
- Small display options such as
interpolation="nearest"can make debugging easier.
Related reading
- Distinguishing overfitting vs good prediction
- Distributed algorithm for SVD?
- Distributed alternatives to hadoop
- Distributed hierarchical clustering
- Distributed cache with Pig and Python
- DistutilsOptionError must supply either home or prefix/exec-prefix -- not both
- DIvisive ANAlysis DIANA Hierarchical Clustering
- Do I have to do one-hot-encoding separately for train and test dataset?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.