Python 2.7.9, matplotlib 1.4.0, ipython 2.3.1, macbook pro retina
I am using ipython's interact() with a 3D plot in an ipython notebook, but find that the graph updates too slowly when changing a slider control. Here is example code that has this problem when run:
from IPython.html.widgets import *
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import math
%matplotlib inline
def plt3Dsin(angle):
npnts = 100
rotationangle_radians = math.radians(angle)
tab_x = np.linspace(0,3,npnts)
tab_y = np.zeros(npnts)
tab_z = np.sin(2.0*np.pi*tab_x)
yrotate = tab_z * np.sin(rotationangle_radians)
zrotate = tab_z * np.cos(rotationangle_radians)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(tab_x, yrotate, zrotate)
ax.set_xlim3d([0.0,3.0])
ax.set_ylim3d([-1.0,1.0])
ax.set_zlim3d([-1.0,1.0])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
interact(plt3Dsin,angle=(0,360,5));
I have tried to separate out the figure and axes creation from actual plotting in the following code, but the first time I change the slider the graph doesn't update and the 2nd time I change it the graph disappears altogether. I assume I'm doing something wrong, but have not been able to figure out what. (The use of globals below is just a quick expedient approach for this simple example code.)
npnts = 100
tab_x = np.linspace(0,3,npnts)
def init_plt3Dsin():
global fig
global ax
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([], [], [], lw=2)
ax.set_xlim3d([0.0,3.0])
ax.set_ylim3d([-1.0,1.0])
ax.set_zlim3d([-1.0,1.0])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
def plt3Dsin(angle):
global fig
global ax
rotationangle_radians = math.radians(angle)
tab_y = np.zeros(npnts)
tab_z = np.sin(2.0*np.pi*tab_x)
yrotate = tab_z * np.sin(rotationangle_radians)
zrotate = tab_z * np.cos(rotationangle_radians)
ax.plot(tab_x, yrotate, zrotate)
init_plt3Dsin()
interact(plt3Dsin,angle=(0,360,5));