Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
2  
It's even better if you can show us what you have tried so far. – Gerrat Nov 11 at 17:46
    
I did a screen capture of my code, you can see it there – Mars. Nov 11 at 18:17
    
You're getting warmer, Mars, but nobody can run screen captures of code. Include any relevant code in a code block within your question. – Gerrat Nov 11 at 18:42

1 Answer 1

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())
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.