Plot 4th dimension with Python
Question: I would to know if there is the possibility to plot in four dimensions using
python. In particular I would to have a tridimensional mesh X, Y, Z and
**f(X,Y,Z) = 1** or **f(X,Y,Z) = 0**. So I need to a symbol (for example "o"
or "x") for some specific point (X,Y,Z). I don't need to a color scale.

Note that I have 100 matrices (512*512) composed by 1 or 0: so my mesh should
be 512*512*100.

I hope I have been clear! Thanks.

EDIT: This is my code:

    
    
    X = np.arange(W.shape[2])
    Y = np.arange(W.shape[1])
    Z = np.arange(W.shape[0])
    X, Y, Z = np.meshgrid(X, Y, Z)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    for z in range(W.shape[0]):
        indexes = np.where(W[z])
        ax.scatter(X[indexes], Y[indexes], ???, marker='.')
    
    ax.set_xlabel('X = columns')
    ax.set_ylabel('Y = rows')
    ax.set_zlabel('Z')
    plt.show()
    

W is my tridimensional matrix, so: **W[0], W[1], etc** are 512x512 matrices.
My question is: what have I to write insted of ??? in my code. I know I
shouldn't ask this, but I can't understand the idea.


Answer: You could create inspect the value of f(x,y,z) for layers of z to see if they
are non-zero or not, and then scatterplot the function based on this.

e.g. for `nz` layers of `(n,n)` matrices, each a slice of a sphere:

    
    
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    n, nz = 48, 24
    x, y = np.linspace(-n//2,n//2-1,n), np.linspace(-n//2,n//2-1,n)
    X, Y = np.meshgrid(x, y)
    
    def f(x,y,z):
        return (X**2 + Y**2 + (z-nz//2)**2) < (n*0.2)**2
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    for z in range(nz):
        layer = f(X, Y, z)
        indexes = np.where(layer)
        ax.scatter(X[indexes], Y[indexes], layer[indexes]*(z-nz//2), marker='.')
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    plt.show()
    

[![enter image description
here](http://i.stack.imgur.com/ShXbg.jpg)](http://i.stack.imgur.com/ShXbg.jpg)

For random non-zero elements of f(x,y,z):

    
    
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    n, nz = 12, 10
    x, y, z = np.linspace(0,n-1,n), np.linspace(0,n-1,n), np.linspace(0,nz-1,nz)
    X, Y, Z = np.meshgrid(x, y, z)
    
    f =  np.random.randint(2, size=(n,n,nz))
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    for z in range(nz):
        indexes = np.where(f[...,z])
        ax.scatter(X[indexes], Y[indexes], f[indexes]+z, marker='.')
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    plt.show()
    

[![enter image description
here](http://i.stack.imgur.com/7zFRJ.png)](http://i.stack.imgur.com/7zFRJ.png)

But with your large arrays, you may run into problems (a) with memory and the
speed of the plotting and (b) being able to resolve detail in the "central"
block of the plot.

