[英]How can i represent a 2d array in 3d in phython?
Im trying to represent an 2 dimensional array of a pyramid in the 3d space.我试图在 3d 空间中表示金字塔的 2 维数组。 In matlab i could just use the function mesh().在matlab中,我可以只使用函数mesh()。 But in python im having a hard time doing it.但是在 python 中,我很难做到。
import numpy as np
import matplotlib.pyplot as plt
Pyramid = np.zeros([512, 512])
x = Pyramid.shape[0]
y = Pyramid.shape[1]
if x != y:
print("ERROR: 'Not square'")
exit()
for i in range(x // 2):
for j in range(i, x - i):
for h in range(i, x - i):
Pyramid[j, h] = i
fig = plt.figure()
ax = plt.axes(projection="3d")
plt.show()
np.meshgrid()
creates the grid coordinates. np.meshgrid()
创建网格坐标。 ax.plot_surface()
plots a 3d height field. ax.plot_surface()
绘制 3d 高度场。
import numpy as np
import matplotlib.pyplot as plt
Pyramid = np.zeros([512, 512])
x = Pyramid.shape[0]
y = Pyramid.shape[1]
for i in range(x // 2):
for j in range(i, x - i):
for h in range(i, x - i):
Pyramid[j, h] = i
fig = plt.figure()
ax = plt.axes(projection="3d")
x2d, y2d = np.meshgrid(range(x), range(y))
ax.plot_surface(x2d, y2d, Pyramid)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('height')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.