python - 如何在 python 中创建 3D 高度图

标签 python matplotlib plot 3d surface

我有一个二维数组 Z,它存储该元素位置的高度。除了使用方法 here其中我需要创建与 Z 大小相同的数组 X 和 Y,是否有更简单的方法来创建 3D 高度图?

3D 表面高度图类似于第一个表面图 here .

最佳答案

这里是matplotlib的代码

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

z = np.array([[x**2 + y**2 for x in range(20)] for y in range(20)])
x, y = np.meshgrid(range(z.shape[0]), range(z.shape[1]))

# show hight map in 3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z)
plt.title('z as 3d height map')
plt.show()

# show hight map in 2d
plt.figure()
plt.title('z as 2d heat map')
p = plt.imshow(z)
plt.colorbar(p)
plt.show()

这里是 z 的 3D 图: enter image description here

这里是 z 的二维图: enter image description here

关于python - 如何在 python 中创建 3D 高度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706919/

相关文章:

python - 使用线条在另一个图形中创建图形并链接它们

python - 使用稀疏数据在Python中绘制二维等值线图

r - 在地 block 中嵌入微型地 block

string - MATLAB 中的整数列表到字符串列表

python - 如何为 Pandas DataFrame _repr_html_ 方法设置默认样式?

python - 无法在 python 3 中导入 matplotlib.pyplot

python - 使用 matplotlib 的年度数据极坐标图

java - 通过 java-%1 在 cmd 中运行 python 文件不是有效的 Win32 应用程序

python - 使用 PyMongo 更新数组中的对象

python - 在 python 中调用 fortran 代码时如何处理全局变量(例如使用 f2py)?