0
点赞
收藏
分享

微信扫一扫

np.meshgrid函数中的indexing参数问题


目录

  • ​​二维meshgrid函数​​
  • ​​三维meshgrid函数​​
  • ​​特别说明​​


meshgrid函数在二维空间中可以简单地理解为将x轴与y轴的每个位置的坐标关联起来形成了一个网格,我们知道空间中的点是由坐标确定的,因此,当x与y关联起来后,我们便可以给与某个点某个特定值并画出对应的图像。具体的可以百度一下,会有很多较为详细的介绍。

这里我想要着重的说一下二维以及三维的​​meshgrid​​​的参数​​indexing​​的问题。

二维meshgrid函数

import numpy as np


class Debug:
def __init__(self):
self.x = np.arange(5)
self.y = np.arange(5)

def grid(self):
X, Y = np.meshgrid(self.x, self.y, indexing="xy")
return X, Y


main = Debug()
X, Y = main.grid()
print("The X grid is:")
print(X)
print("The Y grid is:")
print(Y)
"""
The X grid is:
[[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
The Y grid is:
[[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
"""

从上面的结果可以看出,所获取的网格对应如下图所示,横向为​​x​​​轴,纵向为​​y​​​轴,类似于我们在几何空间中使用的坐标系, 我们通常称之为笛卡尔坐标系(Cartesian coordinate)。在二维​​meshgrid​​网格创建命令中,笛卡尔坐标系是默认的坐标系。

np.meshgrid函数中的indexing参数问题_坐标轴


然而在python编程中,还有一种较为常用的​​indexing​​取法,代码如下:

import numpy as np


class Debug:
def __init__(self):
self.x = np.arange(5)
self.y = np.arange(5)

def grid(self):
X, Y = np.meshgrid(self.x, self.y, indexing="ij")
return X, Y


main = Debug()
i, j = main.grid()
print("The i grid is:")
print(i)
print("The j grid is:")
print(j)
"""
The i grid is:
[[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]
The j grid is:
[[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
"""

此时从上面的结果我们可以看出,所获取的网格对应如下图所示,纵向为​​i​​​轴,横向为​​j​​轴,我们在编程中通常很少使用的这种坐标系。但是它也有自己的优势,这里不进一步说明。

np.meshgrid函数中的indexing参数问题_python_02

三维meshgrid函数

进一步我们讨论三维的情况,代码如下:

import numpy as np


class Debug:
def __init__(self):
self.x = np.arange(3)
self.y = np.arange(3)
self.z = np.arange(3)

def grid(self):
X, Y, Z = np.meshgrid(self.x, self.y, self.z)
return X, Y, Z


main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X)
print("The Y grid is:")
print(Y)
print("The Z grid is:")
print(Z)
"""
The X grid is:
[[[0 0 0]
[1 1 1]
[2 2 2]]

[[0 0 0]
[1 1 1]
[2 2 2]]

[[0 0 0]
[1 1 1]
[2 2 2]]]
The Y grid is:
[[[0 0 0]
[0 0 0]
[0 0 0]]

[[1 1 1]
[1 1 1]
[1 1 1]]

[[2 2 2]
[2 2 2]
[2 2 2]]]
The Z grid is:
[[[0 1 2]
[0 1 2]
[0 1 2]]

[[0 1 2]
[0 1 2]
[0 1 2]]

[[0 1 2]
[0 1 2]
[0 1 2]]]
"""

由上面的结果我们可以看到,此时的坐标轴对应如下图像:

np.meshgrid函数中的indexing参数问题_坐标轴_03


​x​​​轴向下,​​y​​​轴向屏幕内侧,​​z​​​轴向右侧,在三维图像中不再根据​​indexing​​值来区分坐标轴了,而是统一规定了坐标轴的取法,只有对于这个坐标轴的取法深入理解,才能在之后的三维数据处理中游刃有余。

特别说明

但是这里有一个问题,来看一组代码:

class Debug:
def __init__(self):
x = np.array([[[0],
[2]], [[4],
[6]], [[8],
[10]]])
print(x.shape)


main = Debug()
"""
(3, 2, 1)
"""

我们可以看到,输出结果为​​(3, 2, 1)​​​,即沿着​​x​​​轴​​1​​​个元素,沿着​​y​​​轴​​2​​​个元素,沿着​​z​​​轴​​3​​​个元素。再来看一下我们使用​​meshgrid​​方法生成三维网格的情况。

import numpy as np


class Debug:
def __init__(self):
self.x = np.arange(1)
self.y = np.arange(2)
self.z = np.arange(3)

def grid(self):
X, Y, Z = np.meshgrid(self.x, self.y, self.z)
return X, Y, Z


main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X.shape)
print("The Y grid is:")
print(Y.shape)
print("The Z grid is:")
print(Z.shape)
"""
The X grid is:
(2, 1, 3)
The Y grid is:
(2, 1, 3)
The Z grid is:
(2, 1, 3)
"""

我们可以看到,最终输出的​​X,Y,Z​​​的​​shape​​​均为​​(2, 1, 3)​​​,这对应的是沿着​​x​​​轴​​3​​​个元素,沿着​​y​​​轴​​1​​​个元素,沿着​​z​​​轴​​2​​​个元素。突然感觉有些混乱,不符合我们之前想要得到的​​x,y,z​​的排列顺序,为了能够得到正常的排列顺序,我们可以使用如下代码:

import numpy as np


class Debug:
def __init__(self):
self.x = np.arange(1)
self.y = np.arange(2)
self.z = np.arange(3)

def grid(self):
X, Y, Z = np.meshgrid(self.y, self.z, self.x)
return X, Y, Z


main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X.shape)
print("The Y grid is:")
print(Y.shape)
print("The Z grid is:")
print(Z.shape)
"""
The X grid is:
(3, 2, 1)
The Y grid is:
(3, 2, 1)
The Z grid is:
(3, 2, 1)
"""

可以看到运行后我们得到了符合​​Python​​​默认坐标轴习惯的网格形式,这时对应的​​x​​​轴向右侧,​​y​​​轴向下,​​z​​轴向屏幕里面。这个仅仅是为了理解需要,实际操作中无需进行这种坐标轴变换操作,直接使用默认的三维坐标轴方向即可。


举报

相关推荐

0 条评论