简体   繁体   中英

How to use python to plot a 3d surface of a numpy array?

I have a numpy array which is 2 by 3. How can I plot a 3d surface of the 2d array whose x coordinates are the column indexes of the array, y coordinates are the row indexes of the array and z coordinates are the corresponding value of the array. such like this:

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

So I just want to know how to do it. It's good if you can offer the code.

The question is still a bit unclear, but in general for plotting a 3d surface from a 2d array:

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)

Produces:

3d_surface

Or, for your updated question:

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM