這三個函數在本質上是相同的,我們先來研究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()
函數給出的網格陣列為一個完全填充的陣列。網格中每個點的座標x
,y
值均被給出了。
最後我們研究一下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]])]
"""
我們執行後可以發現,三者均可以畫出三維曲面圖,說明三者獲得的網格形式是等價的。並且對比輸出結果,我們可以看到。它們只是在網格座標表示次序上存在差別,在本質上並無差別,都是一樣的。
如果大家覺得有用,請高擡貴手給一個贊讓我上推薦讓更多的人看到吧~