Search code examples
pythondrawmayavimayavi.mlab

How to plot a simple line with mayavi?


Apparently, I do not understand how to plot a simple line with mayavi. According to the docs, I should use mlab.plot3d, where the first three arguments x, y, z contain the coordinates:

x, y, z and s are numpy arrays or lists of the same shape. x, y and z give the positions of the successive points of the line

[quoted from the docs, see the previous link]

So I thought I should give this a try and wrote the following piece of code:

import numpy as np
from mayavi import mlab

fig1 = mlab.figure( bgcolor=(1,1,1), fgcolor=(0,0,0))

myLine = mlab.plot3d( np.array( [2, 3, 4] ),    # x
                      np.array( [1, 1, 1] ),    # y
                      np.array( [1, 1, 1] ),    # z
                      representation='points',
                      line_width=1,
                    )

ax1 = mlab.axes( color=(0,0,0), 
                 nb_labels=4,
                 extent=[0, 5,
                         0, 2,
                         0, 2, ],
               )

mlab.outline(ax1)
mlab.show()

Unfortunately, this does not result in a line, but in a cuboid: enter image description here

Looking at the image, also the starting coordinates are not what I set in the code: I thought it should start at (2,1,1), corresponding to the first value of each of the arrays.

Summarizing, I seem not to understand how to use plot3d. What am I missing?


Solution

  • Documentation in your link suggests to use tube_radius=None

    tube_radius: radius of the tubes used to represent the lines, If None, simple lines are used

    In axes I used nb_labels=5, and [1,5] instead of [0, 5] so it better shows size of line.

    import numpy as np
    from mayavi import mlab
    
    fig1 = mlab.figure(bgcolor=(1,1,1), fgcolor=(0,0,0))
    
    myLine = mlab.plot3d( np.array( [2, 3, 4] ),    # x
                          np.array( [1, 1, 1] ),    # y
                          np.array( [1, 1, 1] ),    # z
                          representation='points',
                          line_width=1,
                          tube_radius=None  # <----
                        )
    
    ax1 = mlab.axes( color=(0,0,0), 
                     nb_labels=5,
                     extent=[1, 5,
                             0, 1,
                             0, 1, ],
                   )
    
    mlab.outline(ax1)
    mlab.show()
    

    enter image description here


    Other example (without mlab.outline(ax1) which may hide line):

    import numpy as np
    from mayavi import mlab
    
    fig1 = mlab.figure(bgcolor=(1,1,1), fgcolor=(0,0,0))
    
    myLine = mlab.plot3d( np.array( [2, 4, 4, 2,  2,  3] ),    # x
                          np.array( [1, 1, 0, 0, .5, .5] ),    # y
                          np.array( [1, 1, 1, 1,  1,  1] ),    # z
                          #representation='points',
                          #line_width=1,
                          tube_radius=None,
                          #tube_radius=0.01,
                        )
    
    ax1 = mlab.axes( color=(0,0,0), 
                     nb_labels=5,
                     extent=[1, 5,
                             0, 1,
                             0, 1, ],
                   )
    
    #mlab.outline(ax1)
    mlab.show()
    

    enter image description here