Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Last active May 13, 2020 22:56
Show Gist options
  • Save yangyushi/12080a7f9bfe015d0bf4ee00a9250c29 to your computer and use it in GitHub Desktop.
Save yangyushi/12080a7f9bfe015d0bf4ee00a9250c29 to your computer and use it in GitHub Desktop.
generate random points on the surface of a sphere
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, scatter, imshow
from mpl_toolkits.mplot3d import Axes3D
N = 1000
z = np.random.uniform(-1, 1, N)
phi = np.random.uniform(-np.pi, np.pi, N)
rxy = np.sqrt(1 - z**2)
x = rxy * np.cos(phi)
y = rxy * np.sin(phi)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, marker='+')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment