Embed Mayavi into a class and show the figure on a Jupyter Notebook cell

280 Views Asked by At

I'm trying to integrate Mayavi with my workflow in Jupyter Notebook. The following stripped-down-to-the-bone code example might seems unnecessary complicated, but those are the restrictions I'm not allowed to change.

The class Test is used to plot something into an mlab figure. The class Wrapper instantiates the Test class, whereas the wrapper_func() function is exposed to the user: it returns the Wrapper instance and should display the mlab figure when the keyword argument show=True.

Note: I'm not allowed to change Wrapper and wrapper_func. Also, the Test class should implement the show() method.

from mayavi import mlab
mlab.init_notebook()
import numpy as np

class Test:
    def __init__(self):
        self.fig = mlab.figure()
        self._add_data()
        
    def _add_data(self):
        pi = np.pi
        cos = np.cos
        sin = np.sin
        dphi, dtheta = pi / 250.0, pi / 250.0
        [phi, theta] = np.mgrid[0:pi + dphi * 1.5:dphi,
                                0:2 * pi + dtheta * 1.5:dtheta]
        m0 = 4; m1 = 3; m2 = 2; m3 = 3
        m4 = 6; m5 = 2; m6 = 6; m7 = 4
        r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + \
            sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7
        x = r * sin(phi) * cos(theta)
        y = r * cos(phi)
        z = r * sin(phi) * sin(theta)
        mlab.mesh(x, y, z, figure=self.fig)
        
    def show(self):
        return self.fig

class Wrapper:
    def __init__(self):
        self.t = Test()
        
    def show(self):
        self.t.show()

def wrapper_func(show=True):
    w = Wrapper()
    if show:
        w.show()
    return w

Then, in a Jupyter Notebook cell the user write:

wrapper_func()

This should display the plot in the output cell. Unfortunately, the figure is not displayed.

Alternatively, the user can do the following:

w = wrapper_func(show=False)
w.show()

What can I do to show the figure in the output cell?

1

There are 1 best solutions below

0
On

when creating the mesh in the Test class, assign it to

self.surf=mlab.mesh( ... )

and return self.surf in show()

in the prgram use as:

t=Test();
t.show()
from mayavi import mlab
mlab.init_notebook()
import numpy as np

class Test:
    def __init__(self):
        self.fig = mlab.figure()
        self._add_data()
        
    def _add_data(self):
        pi = np.pi
        cos = np.cos
        sin = np.sin
        dphi, dtheta = pi / 250.0, pi / 250.0
        [phi, theta] = np.mgrid[0:pi + dphi * 1.5:dphi,
                                0:2 * pi + dtheta * 1.5:dtheta]
        m0 = 4; m1 = 3; m2 = 2; m3 = 3
        m4 = 6; m5 = 2; m6 = 6; m7 = 4
        r = sin(m0 * phi) ** m1 + cos(m2 * phi) ** m3 + \
            sin(m4 * theta) ** m5 + cos(m6 * theta) ** m7
        x = r * sin(phi) * cos(theta)
        y = r * cos(phi)
        z = r * sin(phi) * sin(theta)
        self.surf = mlab.mesh(x, y, z, figure=self.fig)
        
    def show(self):
        return self.surf

t=Test()
t.show()