简体   繁体   中英

Python 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 (eg 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 eg 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.

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM