1
votes

I need to plot spheres that vary in transparency, radius, and x,y,z coordinates.

I am able to plot the spheres and feed in transparency (alpha) values and radius values (which I've changed to random numbers to simplify for this example), but I haven't been able to figure out how to center the spheres around coordinates other then 0,0,0.

How do I center a sphere around coordinates other then 0,0,0 using pythons matplotlib libary?

Here's what I have so far;

from __future__ import print_function
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import time , random

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d([100.0, 0.0])
ax.set_xlabel('X')
ax.set_ylim3d([0.0, 100.0])
ax.set_ylabel('Y')
ax.set_zlim3d([0.0, 100.0])
ax.set_zlabel('Z')
ax.set_title('pFC-amygdala-insula')

wframe = None
tstart = time.time()
for num in range(100):
    oldcol = wframe
    r = random.randint(1, 20)
    alpha = 1.0 / random.randint(25, 100)
    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))
    y = r * np.outer(np.sin(u), np.sin(v))
    z = r * np.outer(np.ones(np.size(u)), np.cos(v))
    sphere = ax.plot_surface(x, y, z, color='b', alpha=alpha)
    if oldcol is not None: ax.collections.remove(oldcol)
    plt.pause(.001)

print('Duration: %f' % (100 / (time.time() - tstart)))
1

1 Answers

2
votes

Just add the coordinates of the center to x, y and z.

x = r * np.outer(np.cos(u), np.sin(v)) + center_x
y = r * np.outer(np.sin(u), np.sin(v)) + center_y
z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + center_z