NumPy meshgrid expands coordinate vectors with an explicit indexing convention

Coordinate vectors can describe the locations along separate directions of a grid. NumPy meshgrid combines them into coordinate arrays suitable for expressions evaluated at many points. Academia Master Trader presents this English lesson for readers in the Central African Republic, using constructed coordinates to explain output shape, indexing conventions and sparse representations.

Two coordinate vectors describe six points

After import numpy as np, define x = np.array([2,4,6]) and y = np.array([10,20]). The assignment X,Y = np.meshgrid(x,y) uses the default indexing convention, xy.

X is [[2,4,6],[2,4,6]], while Y is [[10,10,10],[20,20,20]]. Both arrays have shape (2,3). At a particular row and column, their entries jointly describe one coordinate pair.

For example, position (1,2) represents x equal to six and y equal to twenty. The arrays contain coordinate components, not a list of paired input elements. Meshgrid forms every combination of the supplied x and y coordinates.

NumPy meshgrid coordinate arrays

Default xy indexing places y along rows

With xy indexing, the first output dimension has the length of y, and the second has the length of x. That explains why three x coordinates and two y coordinates produce arrays with two rows and three columns.

X varies across each row, while Y varies down the rows. This convention is useful when relating arrays to Cartesian plotting coordinates, but the chosen convention should always be stated rather than inferred from variable names.

The expression X + Y returns [[12,14,16],[22,24,26]]. Every entry adds the two coordinate components at the same grid position. This is an illustrative function of coordinates, not a rule for combining arbitrary datasets.

Matrix indexing follows the input vector order

The assignment A,B = np.meshgrid(x,y,indexing='ij') returns arrays with shape (3,2). A is [[2,2],[4,4],[6,6]], while B is [[10,20],[10,20],[10,20]].

The coordinate combinations are the same, but their arrangement in array axes differs. With ij indexing, the first input vector varies along the first axis and the second varies along the second axis.

Changing the convention is therefore more than changing a label. Code that indexes or labels the result must use the same axis interpretation as the grid construction.

Sparse coordinates still support a full result

The assignment xs,ys = np.meshgrid(x,y,sparse=True) retains default xy indexing. The shapes are (1,3) for xs and (2,1) for ys. The expression xs + ys broadcasts to the same (2,3) result as X plus Y.

Sparse coordinate arrays keep repeated coordinates implicit. They are not compressed sparse matrices, and an expression involving both directions can still create a complete result array.

Meshgrid copies coordinate data by default. If copy=False is requested, returned arrays may share storage and have repeated references to one memory location. Make copies before writing when independent editable arrays are needed.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record vector lengths, indexing convention and output shapes before interpreting a coordinate grid.