SHARE
TWEET

Untitled

a guest Aug 18th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %matplotlib notebook
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from ipywidgets import interact, IntSlider, RadioButtons
  5. from mpl_toolkits.mplot3d import Axes3D
  6.  
  7. delta = 0.02
  8. x = y = np.arange(-4.0, 4.02, delta)
  9. X, Y = np.meshgrid(x, y)
  10. Z1 = np.exp(-X**2 - Y**2)
  11. Z2 = np.exp(-(X - 1.2)**2 - (Y - 0.7)**2)
  12. Z3 = np.exp(-(X + 0.5)**2 - (Y + 1.4)**2)
  13. Z = (Z1 - Z2 - Z3) * 2
  14. fig = plt.figure(figsize=(9.5, 4))
  15. ax1 = fig.add_subplot(121, projection='3d')
  16. ax2 = fig.add_subplot(122)
  17.  
  18. ax1.plot_surface(X, Y, Z,alpha=0.5, cmap='gnuplot')
  19. l2, = ax1.plot([], [], [], c='m')
  20. l3, = ax1.plot([], [], [], c='g')
  21.  
  22. ax1.set_xlabel('x')
  23. ax1.set_ylabel('y')
  24. line2, = ax2.plot([],[],'m')
  25. line3, = ax2.plot([],[],'g')
  26. ax2.set_xlim(X.min(),X.max())
  27. ax2.set_ylim(-abs(Z).max(),abs(Z).max())
  28. ax2.set_ylabel('Z')
  29.  
  30.  
  31. num = IntSlider(min=0, max=400)
  32. direction = RadioButtons(options = ['x','y'])
  33. @interact(num=num, direction=direction)
  34. def plot(num,direction):
  35.    
  36.     if direction == 'x':
  37.         l3.set_xdata((np.nan))
  38.         l3.set_ydata((np.nan))
  39.         l3.set_3d_properties((np.nan))
  40.         line3.set_xdata([])
  41.         line3.set_ydata([])
  42.        
  43.         l2.set_xdata((X[num,:]))
  44.         l2.set_ydata((Y[num,:]))
  45.         l2.set_3d_properties((Z[num,:]))
  46.        
  47.         line2.set_xdata(X[num,:])
  48.         line2.set_ydata(Z[num,:])
  49.         ax2.set_xlabel('x')
  50.  
  51.    
  52.     else:# direction == 'y':
  53.         l2.set_xdata((np.nan))
  54.         l2.set_ydata((np.nan))
  55.         l2.set_3d_properties((np.nan))
  56.         line2.set_xdata([])
  57.         line2.set_ydata([])
  58.         l3.set_xdata((X[:,num]))
  59.         l3.set_ydata((Y[:,num]))
  60.         l3.set_3d_properties((Z[:,num]))
  61.         line3.set_xdata(Y[:,num])
  62.         line3.set_ydata(Z[:,num])
  63.         ax2.set_xlabel('y')
  64.  
  65. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top