summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorRalf Gommers <ralf.gommers@googlemail.com>2011-12-28 10:30:08 +0100
committerRalf Gommers <ralf.gommers@googlemail.com>2011-12-28 10:30:08 +0100
commit3fb541ef51ca60cf5a903ed83f69120fe3f5373a (patch)
tree43edfe4f0c61db86a0471844f0d55ab119789842 /numpy/lib
parent4e571172a210612bdeba1db0135b7d5fbc44ee0c (diff)
downloadnumpy-3fb541ef51ca60cf5a903ed83f69120fe3f5373a.tar.gz
TST: meshgrid: test expected shapes for Cartesian and matrix indexing.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py16
-rw-r--r--numpy/lib/tests/test_function_base.py13
2 files changed, 23 insertions, 6 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 8e2fc375e..39d1d1c87 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -3215,6 +3215,7 @@ def meshgrid(*xi, **kwargs):
1-D arrays representing the coordinates of a grid.
indexing : {'xy', 'ij'}, optional
Cartesian ('xy', default) or matrix ('ij') indexing of output.
+ See Notes for more details.
sparse : bool, optional
If True a sparse grid is returned in order to conserve memory.
Default is False.
@@ -3238,9 +3239,13 @@ def meshgrid(*xi, **kwargs):
Notes
-----
This function supports both 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. The difference is
- illustrated by the following code snippet::
+ 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 = meshgrid(x, y, sparse=False, indexing='ij')
for i in range(nx):
@@ -3287,8 +3292,6 @@ def meshgrid(*xi, **kwargs):
>>> h = plt.contourf(x,y,z)
"""
- copy_ = kwargs.get('copy', True)
-
if len(xi) < 2:
msg = 'meshgrid() takes 2 or more arguments (%d given)' % int(len(xi) > 0)
raise ValueError(msg)
@@ -3296,8 +3299,11 @@ def meshgrid(*xi, **kwargs):
args = np.atleast_1d(*xi)
ndim = len(args)
+ copy_ = kwargs.get('copy', True)
sparse = kwargs.get('sparse', False)
indexing = kwargs.get('indexing', 'xy')
+ if not indexing in ['xy', 'ij']:
+ raise ValueError("Valid values for `indexing` are 'xy' and 'ij'.")
s0 = (1,) * ndim
output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::]) for i, x in enumerate(args)]
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index eda919df6..57bfee61b 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -1030,7 +1030,9 @@ class TestMeshgrid(TestCase):
assert_raises(ValueError, meshgrid, np.arange(5))
def test_indexing(self):
- [X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], indexing='ij')
+ x = [1, 2, 3]
+ y = [4, 5, 6, 7]
+ [X, Y] = meshgrid(x, y, indexing='ij')
assert_(all(X == array([[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]])))
@@ -1038,6 +1040,15 @@ class TestMeshgrid(TestCase):
[4, 5, 6, 7],
[4, 5, 6, 7]])))
+ # Test expected shapes:
+ z = [8, 9]
+ assert_(meshgrid(x, y)[0].shape == (4, 3))
+ assert_(meshgrid(x, y, indexing='ij')[0].shape == (3, 4))
+ assert_(meshgrid(x, y, z)[0].shape == (4, 3, 2))
+ assert_(meshgrid(x, y, z, indexing='ij')[0].shape == (3, 4, 2))
+
+ assert_raises(ValueError, meshgrid, x, y, indexing='notvalid')
+
def test_sparse(self):
[X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], sparse=True)
assert_(all(X == array([[1, 2, 3]])))