Python 3D Spheres

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

def DrawSphere(xCenter, yCenter, zCenter, r):

u = np.linspace(0, 2 * np.pi, 100)

v = np.linspace(0, np.pi, 100)

x = r * np.outer(np.cos(u), np.sin(v)) + xCenter

y = r * np.outer(np.sin(u), np.sin(v)) + yCenter

z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + zCenter

return (x,y,z)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.set_aspect('equal')

data = np.loadtxt("sphere.txt", dtype="float") # "sphere.txt" has four columns: x y z r

x, y, z, r = data[:,0], data[:,1]data[:,2], data[:,3]

print(data)

scale=10.0

for (xi,yi,zi,ri) in zip(x,y,z,r):

(xs,ys,zs) = DrawSphere(xi,yi,zi,ri)

ax.plot_surface(xs, ys, zs, color='b',alpha=0.5) # need more memory

ax.scatter(xi,yi,zi, s=np.pi*ri**2*scale, c='b', alpha=0.5) #use disks approximate spheres

plt.show()