2017-10-26 3 views
0

Gibt es einen Grund, warum Mayavi nicht zusammen mit ipywidgets in einem Jupyter-Notebook arbeiten wird? Ich kann im Inneren des Notebooks mit 'mlab.init_notebook()', wie so ein x3d MayaVi Bild Inline-Anzeige:Mayavi spielt nicht gut mit ipywidgets in Jupyter notebook

from mayavi import mlab 
 
import numpy as np 
 
mlab.init_notebook() 
 
mlab.clf() 
 
phi, theta = np.mgrid[0:np.pi:11j, 0:2*np.pi:11j] 
 
x = np.sin(phi) * np.cos(theta) 
 
y = np.sin(phi) * np.sin(theta) 
 
z = np.cos(phi) 
 
mlab.mesh(x, y, z) 
 
mlab.mesh(x, y, z, representation='wireframe', color=(0, 0, 0))

Allerdings, wenn ich einen Button (ipywidget) in eine um einen Rückruf Funktion, um die gleiche Mayavi-Figur zu zeichnen, die Handlung zeigt nirgendwo.

from ipywidgets import widgets 
 
from IPython.display import display 
 
from mayavi import mlab 
 
import numpy as np 
 
mlab.init_notebook() 
 

 
def click(a): 
 

 
    mlab.clf() 
 
    phi, theta = np.mgrid[0:np.pi:11j, 0:2*np.pi:11j] 
 
    x = np.sin(phi) * np.cos(theta) 
 
    y = np.sin(phi) * np.sin(theta) 
 
    z = np.cos(phi) 
 
    mlab.mesh(x, y, z) 
 
    mlab.mesh(x, y, z, representation='wireframe', color=(0, 0, 0)) 
 

 
button=widgets.Button(description='Click Me') 
 
button.on_click(click) 
 

 
display(button)

Antwort

0

Sie haben IPython.display.display zu verwenden, um es zu Notebook übertragen zu haben (implizit gemacht, wenn ein MayaVi Objekt der Rückgabewert des letzten mesh() Anruf):

from IPython.display import display 

obj = mlab.mesh(x, y, z) 
display(obj) 

Wie mayavi aktualisiert die Plots für Notebooks nicht interaktiv, Sie müssen die Anzeige im Callback erneut aufrufen. Wenn die Taste mehrmals angeklickt wird, sollten Sie auch die vorherige Grafik mit IPython.display.clear_output:

from IPython import display, clear_output 

def click(a): 
    mlab.clf() 
    ... 
    f = mlab.figure() 
    mlab.mesh(x, y, z) 
    mlab.mesh(x, y, z, representation='wireframe', color=(0, 0, 0)) 
    clear_output(wait=True) 
    # Since we clear outputs, we also need to redisplay button 
    display(button, f) 
löschen