Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0856259

Browse files
committed
Merge pull request numpy#4112 from rhewett/meshgrid_single
ENH: Allow meshgrid to take 1D and 0D inputs
2 parents 86df1b1 + a8e86ab commit 0856259

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

numpy/lib/function_base.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,11 +3125,14 @@ def add_newdoc(place, obj, doc):
31253125
# Based on scitools meshgrid
31263126
def meshgrid(*xi, **kwargs):
31273127
"""
3128-
Return coordinate matrices from two or more coordinate vectors.
3128+
Return coordinate matrices from coordinate vectors.
31293129
31303130
Make N-D coordinate arrays for vectorized evaluations of
31313131
N-D scalar/vector fields over N-D grids, given
31323132
one-dimensional coordinate arrays x1, x2,..., xn.
3133+
3134+
.. versionchanged:: 1.9
3135+
1-D and 0-D cases are allowed.
31333136
31343137
Parameters
31353138
----------
@@ -3178,6 +3181,8 @@ def meshgrid(*xi, **kwargs):
31783181
for i in range(nx):
31793182
for j in range(ny):
31803183
# treat xv[j,i], yv[j,i]
3184+
3185+
In the 1-D and 0-D case, the indexing and sparse keywords have no effect.
31813186
31823187
See Also
31833188
--------
@@ -3214,28 +3219,23 @@ def meshgrid(*xi, **kwargs):
32143219
>>> h = plt.contourf(x,y,z)
32153220
32163221
"""
3217-
if len(xi) < 2:
3218-
raise ValueError(
3219-
'meshgrid() takes 2 or more arguments '
3220-
'(%d given)' % int(len(xi) > 0))
3221-
3222-
args = np.atleast_1d(*xi)
3223-
ndim = len(args)
3222+
ndim = len(xi)
32243223

32253224
copy_ = kwargs.get('copy', True)
32263225
sparse = kwargs.get('sparse', False)
32273226
indexing = kwargs.get('indexing', 'xy')
3227+
32283228
if not indexing in ['xy', 'ij']:
32293229
raise ValueError(
32303230
"Valid values for `indexing` are 'xy' and 'ij'.")
32313231

32323232
s0 = (1,) * ndim
3233-
output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::])
3234-
for i, x in enumerate(args)]
3233+
output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1::])
3234+
for i, x in enumerate(xi)]
32353235

32363236
shape = [x.size for x in output]
32373237

3238-
if indexing == 'xy':
3238+
if indexing == 'xy' and ndim > 1:
32393239
# switch first and second axis
32403240
output[0].shape = (1, -1) + (1,)*(ndim - 2)
32413241
output[1].shape = (-1, 1) + (1,)*(ndim - 2)

numpy/lib/tests/test_function_base.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,28 +1311,33 @@ def test_simple(self):
13111311
class TestMeshgrid(TestCase):
13121312
def test_simple(self):
13131313
[X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7])
1314-
assert_(np.all(X == np.array([[1, 2, 3],
1315-
[1, 2, 3],
1316-
[1, 2, 3],
1317-
[1, 2, 3]])))
1318-
assert_(np.all(Y == np.array([[4, 4, 4],
1319-
[5, 5, 5],
1320-
[6, 6, 6],
1321-
[7, 7, 7]])))
1314+
assert_array_equal(X, np.array([[1, 2, 3],
1315+
[1, 2, 3],
1316+
[1, 2, 3],
1317+
[1, 2, 3]]))
1318+
assert_array_equal(Y, np.array([[4, 4, 4],
1319+
[5, 5, 5],
1320+
[6, 6, 6],
1321+
[7, 7, 7]]))
13221322

13231323
def test_single_input(self):
1324-
assert_raises(ValueError, meshgrid, np.arange(5))
1324+
[X] = meshgrid([1, 2, 3, 4])
1325+
assert_array_equal(X, np.array([1, 2, 3, 4]))
1326+
1327+
def test_no_input(self):
1328+
args = []
1329+
assert_array_equal([], meshgrid(*args))
13251330

13261331
def test_indexing(self):
13271332
x = [1, 2, 3]
13281333
y = [4, 5, 6, 7]
13291334
[X, Y] = meshgrid(x, y, indexing='ij')
1330-
assert_(np.all(X == np.array([[1, 1, 1, 1],
1331-
[2, 2, 2, 2],
1332-
[3, 3, 3, 3]])))
1333-
assert_(np.all(Y == np.array([[4, 5, 6, 7],
1334-
[4, 5, 6, 7],
1335-
[4, 5, 6, 7]])))
1335+
assert_array_equal(X, np.array([[1, 1, 1, 1],
1336+
[2, 2, 2, 2],
1337+
[3, 3, 3, 3]]))
1338+
assert_array_equal(Y, np.array([[4, 5, 6, 7],
1339+
[4, 5, 6, 7],
1340+
[4, 5, 6, 7]]))
13361341

13371342
# Test expected shapes:
13381343
z = [8, 9]
@@ -1345,8 +1350,8 @@ def test_indexing(self):
13451350

13461351
def test_sparse(self):
13471352
[X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], sparse=True)
1348-
assert_(np.all(X == np.array([[1, 2, 3]])))
1349-
assert_(np.all(Y == np.array([[4], [5], [6], [7]])))
1353+
assert_array_equal(X, np.array([[1, 2, 3]]))
1354+
assert_array_equal(Y, np.array([[4], [5], [6], [7]]))
13501355

13511356

13521357
class TestPiecewise(TestCase):

0 commit comments

Comments
 (0)