'Numpy N-D Matrix to a 3D Mesh Graph

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



Solution 1:[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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexblae
Solution 2 Gediz GÜRSU