2025, Sep 25 21:00
Why Matplotlib imshow Shows Only the Array Shape and How to Display the Image in Jupyter and Scripts
Fix Matplotlib imshow not showing the image when only the array shape prints. Load the image, avoid relying on Jupyter auto-display, and use plt.show.
When you work with Matplotlib and an image array, it’s easy to end up seeing only the array’s shape in the output while the image itself never shows up. A typical symptom looks like a tuple such as (320, 240, 3) printed to the console and no rendered picture.
Minimal example that reproduces the issue
The following snippet prints the image shape and attempts to display the picture, but in practice you may only see the tuple:
import matplotlib.pyplot as gfx
print(photo_arr.shape)
gfx.imshow(photo_arr)
gfx.axis('off')
What’s going on
Two things matter here. First, the image must actually be loaded into an array before display. Second, the rendering model depends on how you execute the code. In Notebook, the environment automatically shows the result of the last expression in a cell. If the last line is something other than the image output, you will not see the picture, while any explicit text output, like the shape printed with print(), will still appear. If you make gfx.imshow(photo_arr) the final statement, the image is shown. If you need to render a figure without relying on the “last expression wins” behavior, call gfx.show(). The same explicit call applies when running a normal Python script: use print() for text and gfx.show() to display figures.
Working solution
Ensure the image is read first, then display it and force the render if needed:
import matplotlib.pyplot as gfx
import matplotlib.image as imgio
picture = imgio.imread('img.jpg')
print(picture.shape)
gfx.imshow(picture)
gfx.axis('off')
gfx.show()
Why this detail matters
Understanding what a Notebook auto-displays versus what must be explicitly rendered saves time when debugging visualization code. It also makes behavior consistent between interactive sessions and regular scripts. Loading the data first and controlling when figures are drawn keeps your workflow predictable and your outputs visible where you expect them.
Conclusion
Always read the image before attempting to show it, and don’t rely on implicit rendering if you need deterministic behavior. In a Notebook, either make the plotting call the last line of the cell or call gfx.show(). In scripts, call gfx.show() to ensure the figure appears. With these habits, you’ll avoid the “shape only, no image” surprise and keep your visualization pipeline straightforward.
The article is based on a question from StackOverflow by Lucio Fernandez and an answer by Tchaivotsky.