2

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
3
  • 1
    Using voxels? Commented Aug 20, 2019 at 15:02
  • it seems voxels probably could work but it would require to "hand-code" the pixels intensity in the marker's colors Commented Aug 20, 2019 at 17:27
  • I don't think you need to hand-code anything. You would usually just apply a colormap to your array. Commented Aug 20, 2019 at 17:35

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.