Matplotlib 3D Example 2 : Python

3D Example 2

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from mpl_toolkits.mplot3d import Axes3D
  4.  
  5. np.random.seed(42)
  6.  
  7. n_samples = 500
  8. dim = 3
  9.  
  10. samples = np.random.multivariate_normal(
  11.     np.zeros(dim),
  12.     np.eye(dim),
  13.     n_samples
  14. )
  15.  
  16. for i in range(samples.shape[0]) :
  17.     r = np.power(np.random.random(), 1.0 / 3.0)
  18.     samples[i] *= r / np.linalg.norm(samples[i])
  19.  
  20. upper_samples = []
  21. lower_samples = []
  22. for x, y, z in samples:
  23.     if z > 3 * x + 2 * y - 1:
  24.         upper_samples.append((x, y, z))
  25.     else:
  26.         lower_samples.append((x, y, z))
  27.  
  28. fig = plt.figure('3D scatter plot')
  29. ax = fig.add_subplot(111, projection='3d')
  30. uppers = np.array(upper_samples)
  31. lowers = np.array(lower_samples)
  32.  
  33. ax.scatter(uppers[:, 0], uppers[:, 1], uppers[:, 2], c='r', marker='o')
  34. ax.scatter(uppers[:, 0], uppers[:, 1], uppers[:, 2], c='g', marker='^')
  35.  
  36. plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *