【问题标题】:How can i represent a 2d array in 3d in phython?如何在 python 中以 3d 表示 2d 数组?
【发布时间】:2021-09-21 01:54:18
【问题描述】:

我试图在 3d 空间中表示金字塔的二维数组。在matlab中我可以只使用函数mesh()。但是在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()

【问题讨论】:

  • 有什么问题或错误?
  • 这不是错误。我只是不知道如何在 3d 中表示数组。

标签: python arrays numpy matplotlib 3d


【解决方案1】:

np.meshgrid() 创建网格坐标。 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()

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2020-11-16
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多