Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot 3d numpy array as a 3d image using matplotlib or PIL

After looking at various related answers, I could not figure out a straight way.

I have a 3-d numpy array of shape (390, 280, 160). I want to visualize this as an image. What could be the simplest way to do it?

like image 820
user10050371 Avatar asked Nov 07 '25 03:11

user10050371


1 Answers

Here is a minimal example of volumetric plot using mayavi.
You can find more examples of 3d data visualization here

from mayavi import mlab
import numpy as np

s = np.random.rand(20, 20, 20)
volume = mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=0, vmax=0.8)

mlab.draw()
mlab.savefig('output.png')

enter image description here

From mayavi docs

For such a visualization, tweaking the opacity transfer function is critical to achieve a good effect. Typically, it can be useful to limit the lower and upper values to the 20 and 80 percentiles of the data, in order to have a reasonable fraction of the volume transparent.

like image 142
taras Avatar answered Nov 08 '25 20:11

taras