np.ogrid(),np.mgrid()和meshgrid()函数的关系

这三个函数在本质上是相同的,我们先来研究np.ogrid()函数,代码如下:

# -*- coding: utf-8 -*-
"""
np.ogrid(), np.mgrid(), np.meshgrid()
"""

import numpy as np
import matplotlib.pyplot as plt


class Debug:
    def __init__(self):
        self.x = []
        self.y = []

    def mainProgram(self):
        self.y, self.x = np.ogrid[0:5, 0:5]
        print("The value of x is: ")
        print(self.x)
        print("The value of y is: ")
        print(self.y)
        print("The result of np.ogrid[0:5, 0:5] is: ")
        print(np.ogrid[0:5, 0:5])

        # create a 2D intensity value
        intensity = np.random.random_sample(size=(5, 5))

        fig = plt.figure(1)
        ax = fig.add_subplot(1, 1, 1, projection="3d")
        ax.plot_surface(self.x, self.y, intensity)
        plt.show()


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of x is: 
[[0 1 2 3 4]]
The value of y is: 
[[0]
 [1]
 [2]
 [3]
 [4]]
The result of np.ogrid[0:5, 0:5] is: 
[array([[0],
       [1],
       [2],
       [3],
       [4]]), array([[0, 1, 2, 3, 4]])]
"""

我们可以看到,这里的np.ogrid()会返回一个列表代表的稀疏网格,第一个元素沿着y轴,第二个元素沿着x轴。这与我们之前研究的np.repeat()函数的坐标轴表示是一致的。
接下来我们看一下np.mgrid()函数。代码如下:

# -*- coding: utf-8 -*-
"""
np.ogrid(), np.mgrid(), np.meshgrid()
"""

import numpy as np
import matplotlib.pyplot as plt


class Debug:
    def __init__(self):
        self.x = []
        self.y = []

    def mainProgram(self):
        self.y, self.x = np.mgrid[0:5, 0:5]
        print("The value of x is: ")
        print(self.x)
        print("The value of y is: ")
        print(self.y)
        print("The result of np.mgrid[0:5, 0:5] is: ")
        print(np.mgrid[0:5, 0:5])

        # create a 2D intensity value
        intensity = np.random.random_sample(size=(5, 5))

        fig = plt.figure(1)
        ax = fig.add_subplot(1, 1, 1, projection="3d")
        ax.plot_surface(self.x, self.y, intensity)
        plt.show()


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of x 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 value of y 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 result of np.mgrid[0:5, 0:5] 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]]

 [[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]]]
"""

对比于np.ogrid()函数,这里的np.mgrid()函数给出的网格数组为一个完全填充的数组。网格中每个点的坐标xy值均被给出了。
最后我们研究一下np.meshgrid()。代码如下:

# -*- coding: utf-8 -*-
"""
np.ogrid(), np.mgrid(), np.meshgrid()
"""

import numpy as np
import matplotlib.pyplot as plt


class Debug:
    def __init__(self):
        self.x = []
        self.y = []

    def mainProgram(self):
        x = np.arange(5)
        y = np.arange(5)
        self.x, self.y = np.meshgrid(x, y)
        print("The value of x is: ")
        print(self.x)
        print("The value of y is: ")
        print(self.y)
        print("The result of np.meshgrid() is: ")
        print(np.meshgrid(x, y))

        # create a 2D intensity value
        intensity = np.random.random_sample(size=(5, 5))

        fig = plt.figure(1)
        ax = fig.add_subplot(1, 1, 1, projection="3d")
        ax.plot_surface(self.x, self.y, intensity)
        plt.show()


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of x 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 value of y 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 result of np.meshgrid() is: 
[array([[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]]), array([[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]])]
"""

我们运行后可以发现,三者均可以画出三维曲面图,说明三者获得的网格形式是等价的。并且对比输出结果,我们可以看到。它们只是在网格坐标表示次序上存在差别,在本质上并无差别,都是一样的。

如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/109033156
np