Python 2.7.9、matplotlib 1.4.0、ipython 2.3.1、macbook pro 视网膜
我在 ipython 笔记本中将 ipython 的 interact() 与 3D 图一起使用,但发现更改滑块控件时图形更新太慢。以下是运行时出现此问题的示例代码:
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));
我试图在以下代码中将图形和轴的创建与实际绘图区分开来,但是第一次更改滑块时,图形不会更新,而第二次更改滑块时,图形会完全消失。我认为我做错了什么,但无法弄清楚是什么。(下面使用全局变量只是这个简单示例代码的一种快速权宜之计。)
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));