0

I tried looking this up a lot and there are lot of information on specific examples but they are too specific to understand.

How do I put data in a Numpy N-D Matrix to a 3D graph. please refer below example

 import numpy as np
 X =20

 Y =  20
 Z = 2
 sample = np.zeros(((X,Y,Z)))
 sample[1][2][2]=45
 sample[1][3][0]=52
 sample[1][8][1]=42
 sample[1][15][1]=30
 sample[1][19][2]=15

I Want to use values on X,Y,Z positions to be on a 3D graph (plot).

Thanks in advance

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

 # Define size of data
 P= 25
 X = 70
 Y = 25
 Z = 3

 # Create meshgrid
 x,y = np.meshgrid(np.arange(X),np.arange(Y))

 # Create some random data (your example didn't work)
 sample = np.random.randn((((P,X,Y,Z))))

 # Create figure
 fig=plt.figure()
 ax=fig.add_subplot(111,projection='3d')
 fig.show()

 # Define colors
 colors=['b','r','g']

 # Plot for each entry of in Z
 for i in range(Z):
    ax.plot_wireframe(x, y, sample[:,:,:,i],color=colors[i])
    plt.draw()
 plt.show()

But I only want to draw X,Y,Z only. when I used above code python throws me lots of errors like ValueError: too many values to unpack

D_Wills
  • 345
  • 1
  • 3
  • 14
  • Please provide a [mcve] – Daniel F Feb 16 '17 at 12:02
  • @DanielForsman would this make the question clear – D_Wills Feb 16 '17 at 12:11
  • Explaining what you mean by a "mesh diagram" would be a start. Perhaps a set of test input and expected output. – Daniel F Feb 16 '17 at 12:15
  • @DanielForsman can also be considered as a 3d plot – D_Wills Feb 16 '17 at 12:17
  • Are you looking for a graphical output? Then you probably want to flag `matplotlib` so those folks can help you. – Daniel F Feb 16 '17 at 12:28
  • One of the erros in your code is resulting from `` sample = np.random.randn((((P.X,Y,Z))))``. ``P.X`` should be ``P,X``. Like I said in my answer below, the only way to visualize 4D data is through color-encoding the 4th dimension. Check out (this)[http://stackoverflow.com/questions/14995610/how-to-make-a-4d-plot-with-matplotlib-using-arbitrary-data] answer – alexblae Feb 17 '17 at 14:19
  • Thanks @alexblae , that was a mistake that happend when I tried to copy and paste the code. but now I got it right and there is still the error of too many values to unpack – D_Wills Feb 18 '17 at 00:19

2 Answers2

1

Are you looking for something like this?

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

# Define size of data
X = 20
Y = 20
Z = 3

# Create meshgrid
x,y = np.meshgrid(np.arange(X),np.arange(Y))

# Create some random data (your example didn't work)
sample = np.random.randn(X,Y,Z)

# Create figure
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
fig.show()

# Define colors
colors=['b','r','g']

# Plot for each entry of in Z
for i in range(Z):
    ax.plot_wireframe(x, y, sample[:,:,i],color=colors[i])
    plt.draw()
plt.show()

which would you give

3D wireframe plot

There are plenty of other ways to display 3D data in matplotlib, see also here. However, you are always limited to 3 dimensions (or 4, if you do a 3D scatter plot where color encodes the 4th dimension). So you need to make a decision which dimensions you want to show or if you can summarize them somehow.

alexblae
  • 746
  • 1
  • 5
  • 13
  • This code is exactly what I was looking for but I need it for a 4D matrix. when I edited your code to suit for a 4D matrix (pls look at the edited code in the post) it gives me lots of errors – D_Wills Feb 17 '17 at 12:31
0

I have got something it may work for you. To understand it I explain the process I go briefly. I have connected 4x4x4 = 64 point masses to each other and created a cube with dampers and springs and inner friction. I solved the kinematic and mechanical behaviour using numpy and then I need to visualise the cube all I have is X,Y,Z points for each time step of each mass.

What I have is 4x4x4 XYZ points of a cube for each time tn:

Here how it goes :

import matplotlib.pyplot as plt 

zeroPoint=points[50] # at time step 50 elastic cube in space 
surf0x=zeroPoint[0,:,:,0]
surf0y=zeroPoint[0,:,:,1]
surf0z=zeroPoint[0,:,:,2]

surf1x=zeroPoint[:,0,:,0]
surf1y=zeroPoint[:,0,:,1]
surf1z=zeroPoint[:,0,:,2]

surf2x=zeroPoint[:,:,0,0]
surf2y=zeroPoint[:,:,0,1]
surf2z=zeroPoint[:,:,0,2]

surf3x=zeroPoint[nmx-1,:,:,0]
surf3y=zeroPoint[nmx-1,:,:,1]
surf3z=zeroPoint[nmx-1,:,:,2]

surf4x=zeroPoint[:,nmy-1,:,0]
surf4y=zeroPoint[:,nmy-1,:,1]
surf4z=zeroPoint[:,nmy-1,:,2]

surf5x=zeroPoint[:,:,nmz-1,0]
surf5y=zeroPoint[:,:,nmz-1,1]
surf5z=zeroPoint[:,:,nmz-1,2]

fig = plt.figure(figsize=(10,10))
wf = plt.axes(projection ='3d')
wf.set_xlim(-0.5,2)
wf.set_ylim(-0.5,2)
wf.set_zlim(-0.5,2)
wf.plot_wireframe(surf0x, surf0y, surf0z, color ='green')
wf.plot_wireframe(surf1x, surf1y, surf1z, color ='red')
wf.plot_wireframe(surf2x, surf2y, surf2z, color ='blue')
wf.plot_wireframe(surf3x, surf3y, surf3z, color ='black')
wf.plot_wireframe(surf4x, surf4y, surf4z, color ='purple')
wf.plot_wireframe(surf5x, surf5y, surf5z, color ='orange')
 
 
# displaying the visualization
wf.set_title('Its a Cube :) ')
pyplot.show()

enter image description here

at time step 190 same cube (animation is 60 FPS) :

enter image description here

The trick is as you see you need to create surfaces from points before you go. You dont even need np.meshgrid to do that. People does it for parametric z values calculation. If you have all points you dont need it.

Gediz GÜRSU
  • 555
  • 4
  • 12