2

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.

3
  • 3
    It's even better if you can show us what you have tried so far. Commented Nov 11, 2015 at 17:46
  • I did a screen capture of my code, you can see it there Commented Nov 11, 2015 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. Commented Nov 11, 2015 at 18:42

1 Answer 1

3

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.