Search code examples
pythonmatplotlibmayavimayavi.mlab

Set mayaVI pipeline properties in Python script


I'm using mayaVI to plot a surface and a vector field in 3D, with the functions mayavi.mlab.surf and mayavi.mlab.quiver3D. These functions do not have many keyword arguments that let me modify the appearance of the surface and quivers, in comparison to the Mayavi pipeline, where I can edit things down to the most miniscule detail (for example quiver arrow head radius - see example figure below). The issue is that once I have made these changes in the mayaVI pipeline, there seems to be no way to save these settings until the next time I want to redraw the figure.

I'm in particular interested in editing Contour properties of the surface, and the Glyph Source properties of the vectors (Shaft radius, Tip radius, Tip length).

Question: Is there an easy way to save the Mayavi pipeline settings until next time, or edit them directly in my Python script (i.e. without using the UI)?

Example: enter image description here

Code:

#!/usr/bin/env python
import numpy as np
from mayavi import mlab

xmax = 2.0*np.pi
x, y, z = np.mgrid[-xmax:xmax:25j, -xmax:xmax:25j, -xmax:xmax:1j]

v_x = np.sin(x)*np.cos(y)
v_y = np.cos(x)*np.sin(y) 
v_z = np.zeros_like(z)
v_abs = np.sqrt(v_x**2 + v_y**2) # scalar field

surf = mlab.surf( x[:,:,0], y[:,:,0], v_abs[:,:,0], colormap='magma' )
obj_j = mlab.quiver3d( x[:,:,0], y[:,:,0], z[:,:,-1], v_x[:,:,0], v_y[:,:,0], v_z[:,:,0], mode='arrow')

mlab.show()

Solution

  • For example, to change the tip length of the arrows,

    obj = mlab.quiver3d(..., mode='arrow')
    obj.glyph.glyph_source.glyph_source.tip_length = 0.9
    

    There doesn't seem to be any complete documentation of the mayavi pipeline, but one can guess from the GUI interface about the parameters:

    enter image description here