The Python Book

plot 3d numpy
20160118

A good starting place:

matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

# Simple 3D scatter plot

## Preliminary

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

## Data : create matrix X,Y,Z

``````X=[ [ i for i in range(0,10) ], ]*10
Y=np.transpose(X)

Z=[]
for i in range(len(X)):
R=[]
for j in range(len(Y)):
if i==j: R.append(2)
else: R.append(1)
Z.append(R)``````

X:

``````[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]``````

Y:

``````[[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]])``````

Z:

``````[[2, 1, 1, 1, 1],
[1, 2, 1, 1, 1],
[1, 1, 2, 1, 1],
[1, 1, 1, 2, 1],
[1, 1, 1, 1, 2]]``````

## Scatter plot

``````fig = plt.figure()
ax.scatter(X, Y, Z)
plt.show()``````

# Wireframe plot

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ``` ``````from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np import math # create matrix X,Y,Z X=[ [ i for i in range(0,25) ], ]*25 Y=np.transpose(X) Z=[] for i in range(len(X)): R=[] for j in range(len(Y)): z=math.sin( float(X[i][j])* 2.0*math.pi/25.0) * math.sin( float(Y[i][j])* 2.0*math.pi/25.0) R.append(z) Z.append(R) # plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_wireframe(X, Y, Z) plt.show()``````

Notes by Willem Moors. Generated on momo:/home/willem/sync/20151223_datamungingninja/pythonbook at 2019-07-31 19:22