如何使用python绘制numpy数组的3d表面?

时间:2015-11-11 17:44:39

标签: python arrays numpy 3d

我有一个2比3的numpy数组。 如何绘制2d数组的3d表面,其x坐标是数组的列索引,y坐标是数组的行索引,z坐标是数组的对应值。 像这样:

import numpy as np
twoDArray = np.array([10,8,3],[14,22,36])

# I made a two dimensional array twoDArray
# The first row is [10,8,3] and the second row is [14,22,36]
# There is a function called z(x,y). where x = [0,1], y = [0,1,2]
# I want to visualize the function when 
#(x,y) is (0,0), z(x,y) is 10; (x,y) is (0,1), z(x,y) is 8;  (x,y) is (0,2), z(x,y) is 3
#(x,y) is (1,0), z(x,y) is 14; (x,y) is (1,1), z(x,y) is 22; (x,y) is (1,2), z(x,y) is 36

所以我只想知道怎么做。 如果你能提供代码,那就太好了。

1 个答案:

答案 0 :(得分:1)

这个问题仍然有点不清楚,但总的来说是从二维数组中绘制一个三维表面:

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

x,y = np.meshgrid(np.arange(160),np.arange(120))
z = np.random.random(x.shape)

pl.figure()
ax = pl.subplot(111, projection='3d')
ax.plot_surface(x,y,z)

产地:

3d_surface

或者,对于您更新的问题:

x_1d = np.arange(2)
y_1d = np.arange(3)
x,y = np.meshgrid(x_1d,y_1d)
z = np.array([[10,8,3],[14,22,36]])

pl.figure()
ax = pl.subplot(111, projection='3d')
ax.plot_surface(x,y,z.transpose())