简体   繁体   中英

feeding an array to numpy meshgrid

meshgrid seems to be coded in such a way that each dimension of the meshgrid has to be named individually (eg x and y )

xv, yv = np.meshgrid(x, y)

I am wondering if I can do the same just feeding an array which contains x and y, possibly setting some option of meshgrid to instruct it to use the array the way I need.

I want to do this because it makes life a lot easier when the number of dimensions onto which the meshgrid has to be build is either large or not known a priori.

So the problem is

np.meshgrid([1,2],[10,20])

gives

[array([[1, 2],
        [1, 2]]), array([[10, 10],
        [20, 20]])]

whereas I'd like to to something like

np.meshgrid([[1,2],[10,20]])

but this gives a useless 1D array.

I cannot find any helpful option in the doc

It's a case for Unpacking Argument Lists , and you use the * -operator for this in python:

lst = [[1,2],[10,20]]
np.meshgrid(*lst)

#[array([[1, 2],
#        [1, 2]]), array([[10, 10],
#        [20, 20]])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM