pythonmatplotlibmultidimensional-arraymayavivoxel

Direct volumetric plot 3D array


I am looking to plot a 3-dimensional array. Is there a method that I can directly feed the array, that plots a voxel and with the actual value (color) at the coordinate that arises from the position in the 3d array? All methods I have found so far (e.g. ax.voxels, mlab.points3d...) take an array for x,y and z coordinate each, but I would like to not explicitly state the coordinates, just like one doesn't have to do in plt.imshow.

Edit: I want to plot an array e.g. like this: a = [ [ [10,20], [5,300] ] , [ [7,80],[9,12] ] ] This should give a cube at that has the values inside the array at the coordinates that the indices of the array give. I'd like to know if there's a method that can take such an array directly and plot it.


Solution

  • The question seems to be how to plot an (n,m,l)-shaped array as voxels where the color of the voxels is given by the values of the array. This requires a colormap and a normalization to convert the values to colors (because voxels do not take colormaps as input directly).

    The rest is straight forward:

    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    
    a = np.array( [ [ [10,20], [5,300] ] , [ [7,80],[9,12] ] ] )
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    
    cmap = plt.get_cmap("viridis")
    norm= plt.Normalize(a.min(), a.max())
    ax.voxels(np.ones_like(a), facecolors=cmap(norm(a)), edgecolor="black")
    
    plt.show()
    

    enter image description here