pythonmatplotlib3dmayavimayavi.mlab

How to set parallel perspective on a 3d plot


I have a code in python to render a few spheres in python that looks like this:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import random
import mayavi
from mayavi import mlab

N = 4;
diams = .4*np.ones([N]);
xvals = np.arange(N);
yvals = np.zeros(N);
zvals = np.zeros(N);
pts = mlab.points3d(xvals, yvals, zvals, diams, scale_factor=1,transparent=True)

mlab.show()

The default view of the figure adds distortion based on the camera position (farther spheres smaller). I'd like to set the to parallel projection (farther spheres same size) by some command so it renders like this automatically.

I didn't find a straightforward solution with google or the documentation.


Solution

  • Try setting fig.scene.parallel_projection = True or mlab.gcf().scene.parallel_projection = True in your case.

    As a quick example, compare (zoomed in to magnify differences):

    import numpy as np
    from mayavi import mlab
    
    np.random.seed(1977)
    x, y, z = np.random.random((3, 10))
    
    mlab.points3d(x, y, z)
    mlab.show()
    

    enter image description here

    And when we set an orthogonal projection:

    import numpy as np
    from mayavi import mlab
    
    np.random.seed(1977)
    x, y, z = np.random.random((3, 10))
    
    mlab.points3d(x, y, z)
    mlab.gcf().scene.parallel_projection = True
    mlab.show()
    

    enter image description here