odd behaviour of meshgrid with 3 arrays

Hi all, I was trying to use meshgrid with three arrays and got some odd results. Here's a simple example: xt = np.array([1,2,3,4]) yt = np.array([6,7,8]) zt = np.array([12,13]) xxx,yyy,zzz = np.meshgrid(xt,yt,zt) So I would expect that xxx[0,0,:] = array([1,2,3,4]) instead I get xxx[0,0,:] = array([1,1]) and xxx[0,:,0] = array([1,2,3,4]) also yyy[:,0,0] = array([6,7,8]), whereas I would expect yyy[0,:,0] = array([6,7,8]) So what's going on? This seems like a bug to me. Any suggestions for getting what I wanted -- i.e. xxx.shape = (2,3,4), with values as appropriate? Thanks, Jon -- Jonathan D. Slavin (he/him) Astrophysicist - High Energy Astrophysics Division Center for Astrophysics | Harvard & Smithsonian Office: (617) 496-7981 | Cell: (781) 363-0035 60 Garden Street | MS 04 | Cambridge, MA 02138

On Thu, Nov 21, 2024 at 2:00 PM Slavin, Jonathan via NumPy-Discussion < numpy-discussion@python.org> wrote:
Hi all,
I was trying to use meshgrid with three arrays and got some odd results. Here's a simple example: xt = np.array([1,2,3,4]) yt = np.array([6,7,8]) zt = np.array([12,13]) xxx,yyy,zzz = np.meshgrid(xt,yt,zt) So I would expect that xxx[0,0,:] = array([1,2,3,4]) instead I get xxx[0,0,:] = array([1,1]) and xxx[0,:,0] = array([1,2,3,4]) also yyy[:,0,0] = array([6,7,8]), whereas I would expect yyy[0,:,0] = array([6,7,8]) So what's going on? This seems like a bug to me. Any suggestions for getting what I wanted -- i.e. xxx.shape = (2,3,4), with values as appropriate?
This is documented in the `Notes` section concerning the behavior with the default `indexing='xy'`, which is primarily for 2D arrays (and presumably following a convention from another language like MATLAB). In order to get the (2, 3, 4) arrays that you want, use `indexing='ij'` # Note the flipped order of inputs and outputs since you want the `zt` values across the first axis. zzz, yyy, xxx = np.meshgrid(zt, yt, xt, indexing='ij') -- Robert Kern

Hi Robert, Thanks. I discovered that after I posted. It had been noted as a bug at one point I see. I still kinda feel like it's a bug since the standard way to call it results in very unexpected behavior. But in any case I now know how to get the results I was looking for. Regards, Jon On Thu, Nov 21, 2024 at 2:18 PM Robert Kern <robert.kern@gmail.com> wrote:
On Thu, Nov 21, 2024 at 2:00 PM Slavin, Jonathan via NumPy-Discussion < numpy-discussion@python.org> wrote:
Hi all,
I was trying to use meshgrid with three arrays and got some odd results. Here's a simple example: xt = np.array([1,2,3,4]) yt = np.array([6,7,8]) zt = np.array([12,13]) xxx,yyy,zzz = np.meshgrid(xt,yt,zt) So I would expect that xxx[0,0,:] = array([1,2,3,4]) instead I get xxx[0,0,:] = array([1,1]) and xxx[0,:,0] = array([1,2,3,4]) also yyy[:,0,0] = array([6,7,8]), whereas I would expect yyy[0,:,0] = array([6,7,8]) So what's going on? This seems like a bug to me. Any suggestions for getting what I wanted -- i.e. xxx.shape = (2,3,4), with values as appropriate?
This is documented in the `Notes` section concerning the behavior with the default `indexing='xy'`, which is primarily for 2D arrays (and presumably following a convention from another language like MATLAB). In order to get the (2, 3, 4) arrays that you want, use `indexing='ij'`
# Note the flipped order of inputs and outputs since you want the `zt` values across the first axis. zzz, yyy, xxx = np.meshgrid(zt, yt, xt, indexing='ij')
-- Robert Kern
participants (2)
-
Robert Kern
-
Slavin, Jonathan