Image Display with imshow(Z)ΒΆ

The imshow() method displays a 2D array as a rasterized image, mapping each array element to a pixel with color determined by a colormap. It treats the input array as a regular grid where each cell has equal size, making it the fastest and simplest way to visualize matrix-like data.

Why this matters for data science: imshow() is a workhorse function in data science and ML. It is used to display actual images (photographs, medical scans), visualize confusion matrices, show correlation heatmaps, inspect weight matrices of neural networks, display feature maps from convolutional layers, and visualize any 2D array data. The origin='lower' parameter flips the y-axis so that row 0 appears at the bottom (matching mathematical convention) rather than the top (which is the default image convention). The 16x16 grid used here produces visible pixels, making the discrete nature of the data clear – in practice, higher resolution arrays produce smoother-looking images. Unlike pcolormesh(), imshow() requires evenly spaced data and always maps to a rectangular pixel grid.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery-nogrid')

# make data
X, Y = np.meshgrid(np.linspace(-3, 3, 16), np.linspace(-3, 3, 16))
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)

# plot
fig, ax = plt.subplots()

ax.imshow(Z, origin='lower')

plt.show()