Python 2.7.9、matplotlib 1.4.0、ipython 2.3.1、macbook プロ網膜
ipython ノートブックの 3D プロットで ipython の interact() を使用していますが、スライダー コントロールを変更するとグラフの更新が遅すぎることがわかりました。実行時にこの問題が発生するコードの例を次に示します。
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));
次のコードで、図と軸の作成を実際のプロットから分離しようとしましたが、最初にスライダーを変更するとグラフが更新されず、2 回目に変更するとグラフが完全に消えます。私は何か間違ったことをしていると思いますが、何を理解することができませんでした。(以下のグローバルの使用は、この単純なサンプル コードの簡単な方法です。)
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));