How to update mayavi interactively in ipython notebook?

Viewed 567

I've been trying to create an interactive 3d plot in jupyter notebook.This worked great with matplotlib. Unfortunately matplotlib is very unfit to display intersecting planes. Therefore I wanted to switch to mayavi.

However mayavi shows some weird behaviour when I try to interactively change its values. Running the following Code:

%matplotlib notebook

#math
import numpy as np


#plotting
from mayavi import mlab
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from IPython import display
import time

mlab.init_notebook()
X = np.arange(-5, 5, 0.3)
Y = np.arange(-5, 5, 0.3)
X,Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)

mlab.clf()

#plot setup
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def foo(x, y):
    display.clear_output()
    ax.cla()
    #generate plots
    ax.pcolor(X,Y,R,cmap='Blues')
    ax.plot(x, y, 'ro')
    mlab.mesh(X + x,Y,R, colormap='Blues')

    #display plots
    display.display(plt.gcf())
    display.display(mlab.gcf())

def op_on_click(event):
    foo(event.xdata, event.ydata)

foo(3,0)
time.sleep(10)
foo(10,0)
fig.canvas.mpl_connect('button_press_event', op_on_click)

It will immediately display the mayavi plot and update it after 10 seconds (as expected). But it does not redraw when the on_click event of the matplotlib figure is triggered. Running it without mlab.clf() shows that the meshes actually get generated, the plot just doesnt get redrawn.

Any help on how to make it react to the on_click would be very appreciated.

0 Answers
Related