plt14_3d.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. # 14 - 3d
  5. """
  6. Please note, this script is for python3+.
  7. If you are using python2+, please modify it accordingly.
  8. Tutorial reference:
  9. http://www.python-course.eu/matplotlib_multiple_figures.php
  10. """
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. from mpl_toolkits.mplot3d import Axes3D
  14. fig = plt.figure()
  15. ax = Axes3D(fig)
  16. # X, Y value
  17. X = np.arange(-4, 4, 0.25)
  18. Y = np.arange(-4, 4, 0.25)
  19. X, Y = np.meshgrid(X, Y)
  20. R = np.sqrt(X ** 2 + Y ** 2)
  21. # height value
  22. Z = np.sin(R)
  23. ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
  24. """
  25. ============= ================================================
  26. Argument Description
  27. ============= ================================================
  28. *X*, *Y*, *Z* Data values as 2D arrays
  29. *rstride* Array row stride (step size), defaults to 10
  30. *cstride* Array column stride (step size), defaults to 10
  31. *color* Color of the surface patches
  32. *cmap* A colormap for the surface patches.
  33. *facecolors* Face colors for the individual patches
  34. *norm* An instance of Normalize to map values to colors
  35. *vmin* Minimum value to map
  36. *vmax* Maximum value to map
  37. *shade* Whether to shade the facecolors
  38. ============= ================================================
  39. """
  40. # I think this is different from plt12_contours
  41. ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))
  42. """
  43. ========== ================================================
  44. Argument Description
  45. ========== ================================================
  46. *X*, *Y*, Data values as numpy.arrays
  47. *Z*
  48. *zdir* The direction to use: x, y or z (default)
  49. *offset* If specified plot a projection of the filled contour
  50. on this position in plane normal to zdir
  51. ========== ================================================
  52. """
  53. ax.set_zlim(-2, 2)
  54. plt.show()