4

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?

1
  • Images represent 2D data. How do you expect a visualization of 3D data to appear? Commented Aug 22, 2018 at 12:27

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried with mayavi on jupyter notebook but it seems to be not working. Is it possible to do it in an alternative way?
@user10050371, apparently neither matplotlib not PIL is not intended for 3d visualization. Did you follow this guide for mayavi with jupyter?
I tried it by saving the figure, using mlab.savefig('a.png'). It saves an image but the image is homogeneous and has full green color whereas random generator would not have this structure.
@user10050371, Try adding mlab.draw() before mlab.save(). I think I've had the same issue as well.
@user10050371, I updated my answer with the output image.
|

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.