Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %matplotlib notebook
- import numpy as np
- import matplotlib.pyplot as plt
- from ipywidgets import interact, IntSlider, RadioButtons
- from mpl_toolkits.mplot3d import Axes3D
- delta = 0.02
- x = y = np.arange(-4.0, 4.02, delta)
- X, Y = np.meshgrid(x, y)
- Z1 = np.exp(-X**2 - Y**2)
- Z2 = np.exp(-(X - 1.2)**2 - (Y - 0.7)**2)
- Z3 = np.exp(-(X + 0.5)**2 - (Y + 1.4)**2)
- Z = (Z1 - Z2 - Z3) * 2
- fig = plt.figure(figsize=(9.5, 4))
- ax1 = fig.add_subplot(121, projection='3d')
- ax2 = fig.add_subplot(122)
- ax1.plot_surface(X, Y, Z,alpha=0.5, cmap='gnuplot')
- l2, = ax1.plot([], [], [], c='m')
- l3, = ax1.plot([], [], [], c='g')
- ax1.set_xlabel('x')
- ax1.set_ylabel('y')
- line2, = ax2.plot([],[],'m')
- line3, = ax2.plot([],[],'g')
- ax2.set_xlim(X.min(),X.max())
- ax2.set_ylim(-abs(Z).max(),abs(Z).max())
- ax2.set_ylabel('Z')
- num = IntSlider(min=0, max=400)
- direction = RadioButtons(options = ['x','y'])
- @interact(num=num, direction=direction)
- def plot(num,direction):
- if direction == 'x':
- l3.set_xdata((np.nan))
- l3.set_ydata((np.nan))
- l3.set_3d_properties((np.nan))
- line3.set_xdata([])
- line3.set_ydata([])
- l2.set_xdata((X[num,:]))
- l2.set_ydata((Y[num,:]))
- l2.set_3d_properties((Z[num,:]))
- line2.set_xdata(X[num,:])
- line2.set_ydata(Z[num,:])
- ax2.set_xlabel('x')
- else:# direction == 'y':
- l2.set_xdata((np.nan))
- l2.set_ydata((np.nan))
- l2.set_3d_properties((np.nan))
- line2.set_xdata([])
- line2.set_ydata([])
- l3.set_xdata((X[:,num]))
- l3.set_ydata((Y[:,num]))
- l3.set_3d_properties((Z[:,num]))
- line3.set_xdata(Y[:,num])
- line3.set_ydata(Z[:,num])
- ax2.set_xlabel('y')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement