1

I am trying to display a 3d array - basically, a time-sequence of 2d images - like this : enter image description here

Based on some code I found on SO, my closest solution so far gives me this (using scatter plot and big squared markers) : enter image description here

but the squared markers are not aligned along the 3 axes. So I am asking:
- if their is a workaround to make the markers aligned (and still as the plot is rotated)
- or/and if their is prettier way ?

Here's the code :

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

N = 8

volume = np.random.rand(N, N, N)

x = np.arange(volume.shape[0])[:, None, None]
y = np.arange(volume.shape[1])[None, :, None]
z = np.arange(volume.shape[2])[None, None, :]
x, y, z = np.broadcast_arrays(x, y, z)

c = np.tile(volume.ravel()[:, None], [1, 3])

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x.ravel(),
       y.ravel(),
       z.ravel(),
       c=c,
       s=500, # marker's size
      marker="s") # squared markers
mocquin
  • 402
  • 3
  • 11

1 Answers1

3

Here's the result I'm satisfied with, using voxels as suggested : enter image description here

%matplotlib qt

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

N = 4
volume = np.random.rand(N, N, N)
filled = np.ones((N, N, N), dtype=np.bool)

# repeating values 3 times for grayscale
colors = np.repeat(volume[:, :, :, np.newaxis], 3, axis=3)

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.voxels(filled, facecolors=colors, edgecolors='k')
plt.show()
mocquin
  • 402
  • 3
  • 11