0

I'm trying to plot a cubic 3D scene-grid stored in a 3D matrix data which contains indices (eg [0..10]) which indicate in which color the voxel is plotted, where 0 is the "empty" label. In Matlab I use the following code:

[X,Y,Z] = meshgrid(1:size(data,1),1:size(data,2),1:size(data,3));
xyz=[X(:) Y(:) Z(:)];
idx = data~=0;   % Ignore labels 0 -> they specify empty voxels
pcshow(xyz(idx,:),data(idx),'MarkerSize',1000)

Are there similar functions in python?

This stackoverflow-answer is close but it lacks the possibility to pass the data matrix. A hack would be to loop over all labels, but I'm looking for a more elegant solution.

mcExchange
  • 6,154
  • 12
  • 57
  • 103

1 Answers1

0

Here is some python solution:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X, Y, Z = np.meshgrid(np.arange(data.shape[0]), np.arange(data.shape[1]), np.arange(data.shape[2]))
[x, y, z] = (np.reshape(X, (-1)), np.reshape(Y, (-1)), np.reshape(Z, (-1)))
idx = (data != 0) 
idx = np.reshape(idx, (-1))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
scat = ax.scatter(x[idx], y[idx], z[idx], c=np.reshape(data,(-1))[idx], cmap='jet', marker="s")

# Some additional formatting
ax.set_xlabel('X', fontsize=9)
ax.set_ylabel('Y', fontsize=9)
ax.set_zlabel('Z', fontsize=9)

# Setting camera angle
az = 150
el = 30
ax.view_init(elev=el, azim=az)
fig.colorbar(scat, shrink=0.5, aspect=5)
mcExchange
  • 6,154
  • 12
  • 57
  • 103