ML Foundations

Choose NumPy meshgrid indexing to match your axis order

NumPy meshgrid defaults to Cartesian xy indexing, which swaps the first two dimensions. Use indexing="ij" when output axes should follow input coordinate vectors.

Example results for Choose NumPy meshgrid indexing to match your axis order. xy shapes: (2, 3) (2, 3)
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

The default is Cartesian indexing

`numpy.meshgrid(x, y)` defaults to `indexing="xy"`. In two dimensions this follows Cartesian plotting convention: the first coordinate varies across columns and the second across rows. If `x` has length M and `y` has length N, each dense output has shape `(N, M)`. That shape can be surprising when the coordinate vectors are intended to correspond directly to array axes. [1]

The setting changes axis order, not the coordinate values themselves. A calculation can therefore produce plausible numbers while placing them in a transposed grid. Before combining coordinate matrices with a field array, compare the grid shape with the the field axis convention. The example uses unequal vector lengths so the two output shapes cannot look accidentally identical. [1]

References: [1]

Use ij indexing when dimensions follow input axes

With `indexing="ij"`, NumPy preserves the input-vector axis order: for the same lengths M and N, the dense outputs have shape `(M, N)`. This matches matrix-style indexing, where the first index addresses axis 0 and the second index addresses axis 1. NumPy describes the alternative as matrix (`ij`) indexing. [1]

The code builds grids both ways from an x vector of length three and a y vector of length two. The default produces `(2, 3)` arrays; the `ij` form produces `(3, 2)`. It then retrieves the same coordinate pair `(30, 2)` using indices appropriate to each layout. This is an executed shape-and-coordinate check, not a plot or performance benchmark. [1, 2]

Executed with Python 3.12.14 and NumPy 2.3.5 on CPU; assertions passed.

"""Compare meshgrid's Cartesian and matrix indexing axis order."""
import numpy as np

x = np.array([10, 20, 30])
y = np.array([1, 2])
x_xy, y_xy = np.meshgrid(x, y)
x_ij, y_ij = np.meshgrid(x, y, indexing="ij")

print("xy shapes:", x_xy.shape, y_xy.shape)
print("ij shapes:", x_ij.shape, y_ij.shape)
print("xy coordinate at row 1, column 2:", x_xy[1, 2], y_xy[1, 2])
print("ij coordinate at axis-0 2, axis-1 1:", x_ij[2, 1], y_ij[2, 1])

assert x_xy.shape == (2, 3) and y_xy.shape == (2, 3)
assert x_ij.shape == (3, 2) and y_ij.shape == (3, 2)
assert (x_xy[1, 2], y_xy[1, 2]) == (30, 2)
assert (x_ij[2, 1], y_ij[2, 1]) == (30, 2)

Recorded output

xy shapes: (2, 3) (2, 3)
ij shapes: (3, 2) (3, 2)
xy coordinate at row 1, column 2: 30 2
ij coordinate at axis-0 2, axis-1 1: 30 2

References: [1], [2]

Check grid shape before broadcasting or indexing

For a two-dimensional field whose first axis corresponds to x and second axis to y, use `indexing="ij"` and verify the resulting shape before evaluating the field. If the intended result is an x-horizontal/y-vertical Cartesian plot, the default `xy` convention is often convenient. The right setting depends on how downstream arrays name their axes, so make that choice explicit where a silent transpose would be costly. [1]

NumPy also provides `indices` for constructing index grids from a shape; its leading dimension identifies each grid axis. That offers a useful matrix-indexing mental model, but it does not replace choosing `meshgrid` indexing for coordinate vectors. This guide covers only dense two-dimensional grids with one-dimensional inputs; sparse grids, higher dimensions and copy/layout behavior need separate checks. [1, 2]

References: [1], [2]

Sources

  1. NumPy: meshgrid
  2. NumPy: indices