Matplotlib 3D Example : Python

3D Example

  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_grids = 51
  8. c = n_grids / 2
  9. nf = 2
  10.  
  11. x = np.linspace(0, 1, n_grids)
  12. y = np.linspace(0, 1, n_grids)
  13. X, Y = np.meshgrid(x, y)
  14.  
  15. spectrum = np.zeros((n_grids, n_grids), dtype=np.complex)
  16. noise = [np.complex(x, y) for x, y in np.random.uniform(-1, 1, ((2 * nf + 1) ** 2 / 2, 2))]
  17. noisy_block = np.concatenate((noise, [0j], np.conjugate(noise[:: -1])))
  18. spectrum[c - nf: c + nf + 1, c - nf: c + nf + 1] = noisy_block.reshape((2 * nf + 1, 2 * nf + 1))
  19. Z = np.real(np.fft.ifft2(np.fft.ifftshift(spectrum)))
  20.  
  21. fig = plt.figure('3D surface & wire')
  22. ax = fig.add_subplot(1, 2, 1, projection='3d')
  23. ax.plot_surface(X, Y, Z, alpha=0.7, cmap='jet', rstride=1, cstride=1, lw=0)
  24.  
  25. ax = fig.add_subplot(1, 2, 2, projection='3d')
  26. ax.plot_wireframe(X, Y, Z, rstride=3, cstride=3, lw=0.5)
  27.  
  28. plt.show()

Leave a Reply

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