forked from ncrump/PyPlots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Sphere Example.py
More file actions
34 lines (28 loc) · 919 Bytes
/
Copy pathRandom Sphere Example.py
File metadata and controls
34 lines (28 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Generates random uniformly distributed points on the surface of a unit sphere
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#****************************************************
# number of points to generate
n = 1000
# generates randomly distributed theta,phi points at r = 1
r = np.ones(n)
# this prevents clumping at poles for theta,phi
theta0 = np.random.uniform(-1,1,n)
theta = np.arccos(theta0)
#theta = np.random.uniform(0,2*np.pi,n)
phi = np.random.uniform(0,2*np.pi,n)
# converts spherical [r,theta,phi] points to cartesian [x,y,z] points
x = r*np.sin(theta)*np.cos(phi)
y = r*np.sin(theta)*np.sin(phi)
z = r*np.cos(theta)
# plots distribution of points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x,y,z,'bo')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#****************************************************