This always confuses me. Here’s the help on meshgrid:
meshgrid supports two indexing conventions through the indexing
keyword argument. Giving the string 'ij' returns a meshgrid with
matrix indexing, while 'xy' returns a meshgrid with Cartesian indexing.
In the 2-D case with inputs of length M and N, the outputs are of shape
(N, M) for 'xy' indexing and (M, N) for 'ij' indexing.
In the 3-D case with inputs of length M, N and P,
outputs are of shape (N, M, P) for
'xy' indexing and (M, N, P) for 'ij' indexing.
The difference is illustrated by the following code snippet::
xv, yv = np.meshgrid(x, y, indexing='ij')
for i in range(nx):
for j in range(ny):
# treat xv[i,j], yv[i,j]
xv, yv = np.meshgrid(x, y, indexing='xy')
for i in range(nx):
for j in range(ny):
# treat xv[j,i], yv[j,i]
Here is an example, first with Cartesian, default, coordinates. The shape of A is len(b), len(a), because a is laid out in a row and so defines the number of columns!
import numpy as npa = np.array([0,1,2,3])b = np.array([0, 5, 10])A, B = np.meshgrid(a,b, indexing='xy')A, B, A.shape, A[0], A[:, 0]
The mean function returns the average of the array “along the given axis”. I don’t find that helpful. Remember: if you average along an axis you remove that axis.
If you average along axis=0 you get one value per axis 1. If you average along a axis 1, you get one value per row. Thus:
print('Average along axis 0, len(a)', A.mean(0))print('Average along axis 1, len(b)', A.mean(1))
Average along axis 0, len(a) [0. 1. 2. 3.]
Average along axis 1, len(b) [1.5 1.5 1.5]