2025, Dec 11 11:00

Highlight a discrete path on a 5×5 grid using a 2D mask and matplotlib, with an optional RGB-array variant

Learn how to plot a path on a 5×5 grid in Python with matplotlib: turn coordinates into a 2D mask, use a discrete colormap, align gridlines, or use an RGB array

Rendering a discrete path on top of a small grid doesn’t require a heavy plotting setup. If you have the sequence of grid coordinates, you can turn it into a simple mask and let matplotlib do the rest: color the path differently from the empty cells and draw the gridlines so every cell is clearly visible.

Problem setup

The path is a sequence of grid coordinates for a 5×5 board. Here is the exact input:

route_cells = [(4, 4), (4, 3), (3, 3), (2, 3), (2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]

What actually stands in the way

The only non-obvious bit is how to convert that list of tuples into something a plotting library can paint efficiently and how to make the grid appear as cell-aligned lines. With matplotlib, you don’t need to draw each rectangle manually. A 2D array plus a discrete colormap is enough. One value represents background, another value represents the path. Gridlines are enabled directly on the axes, and placing ticks at half-integers aligns the lines to the cell borders.

Minimal solution with matplotlib

The snippet below transforms the path into a 5×5 mask, uses a two-color map to distinguish the path from the rest, and draws the gridlines on top.

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
# Path encoded as grid coordinates
route_cells = [(4, 4), (4, 3), (3, 3), (2, 3), (2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]
# Build a 5x5 mask: 1 for path cells, 0 for background
mask_grid = np.zeros((5, 5))
for i, j in route_cells:
    mask_grid[i][j] = 1
# Two discrete colors mapped to values 0 and 1
palette = colors.ListedColormap(["red", "blue"])  # any contrasting colors work
levels = [0, 1, 2]  # boundary at value >= 1
scale = colors.BoundaryNorm(levels, palette.N)
# Plot the grid with the highlighted path
fig, ax = plt.subplots()
ax.imshow(mask_grid, cmap=palette, norm=scale)
# Draw cell-aligned gridlines
ax.grid(which="major", axis="both", linestyle="-", color="k", linewidth=2)
ax.set_xticks(np.arange(-.5, 5, 1))
ax.set_yticks(np.arange(-.5, 5, 1))
plt.show()

This directly achieves a contrasting path on a 5×5 lattice without any extra drawing primitives. The color choices are arbitrary; you only need two clearly distinguishable colors.

Optional variant without a colormap

If you only need specific colors such as white and red, you can skip the colormap layer and directly paint RGB values. The idea is to create a 5×5×3 array prefilled with white and overwrite the path cells with red. You can also set the extent on imshow to avoid manually placing tick positions.

import matplotlib.pyplot as plt
from matplotlib.colors import to_rgb
import numpy as np
# Path as before
route_cells = [(4, 4), (4, 3), (3, 3), (2, 3), (2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (1, 2), (1, 1), (1, 0), (0, 0)]
# Pre-fill with white, then paint the path cells red
rgb_field = np.ones((5, 5, 3))
for i, j in route_cells:
    rgb_field[i, j] = to_rgb("red")
fig, ax = plt.subplots()
ax.imshow(rgb_field, extent=[0, 5, 0, 5])
plt.show()

Why it’s worth knowing

Encoding categorical information as a small numeric grid scales cleanly from toy examples to real tasks. You avoid per-cell drawing loops and keep control over visual contrast and alignment with a few lines. The same pattern works for paths, masks, and any binary or discrete overlay on a grid.

Takeaways

Represent the path as a binary mask in a 2D array, use a discrete color mapping to separate background and path, and align gridlines by placing ticks at half-integers. If all you need are fixed colors, painting an RGB array is an even leaner option. Both approaches stay compact, readable, and do exactly what’s needed for highlighting a path on a grid.