3D散点图

演示3D中的基本散点图。

3D散点图示例

  1. # This import registers the 3D projection, but is otherwise unused.
  2. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. # Fixing random state for reproducibility
  6. np.random.seed(19680801)
  7. def randrange(n, vmin, vmax):
  8. '''
  9. Helper function to make an array of random numbers having shape (n, )
  10. with each number distributed Uniform(vmin, vmax).
  11. '''
  12. return (vmax - vmin)*np.random.rand(n) + vmin
  13. fig = plt.figure()
  14. ax = fig.add_subplot(111, projection='3d')
  15. n = 100
  16. # For each set of style and range settings, plot n random points in the box
  17. # defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
  18. for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
  19. xs = randrange(n, 23, 32)
  20. ys = randrange(n, 0, 100)
  21. zs = randrange(n, zlow, zhigh)
  22. ax.scatter(xs, ys, zs, c=c, marker=m)
  23. ax.set_xlabel('X Label')
  24. ax.set_ylabel('Y Label')
  25. ax.set_zlabel('Z Label')
  26. plt.show()

下载这个示例