4

How can we make spheres of radius R centered at given coordinates(x,y,z). Like if there are 10 set of coordinates for the centers of the spheres and correspondingly 10 different values of the radii. How can we plot it in python ? Is there any way to do this in python ,like in MATLAB this can be done using surf command. In python how can we achieve this ?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Simrandeep Bahal
  • 109
  • 1
  • 2
  • 8
  • Can you provide sample plot. Do you just want to plot a single sphere or 10 sphere? You may provide sample input and output. – Lawhatre Nov 03 '20 at 06:18
  • Need to plot 10 spheres. The x ,y and z coordinates of the center of the 10 spheres are ((1,1,2),(2,3,4),(1,7,6),(5,6,4).... and so on). The corresponding radii of the spheres are (5,2,9,4..... ,6) – Simrandeep Bahal Nov 03 '20 at 06:58
  • will scatter plot work for you? or you need surf plot ? – Lawhatre Nov 03 '20 at 07:03
  • Surf plot is desired first. In scatter plot how will you make the radius values as specified by the user ? – Simrandeep Bahal Nov 03 '20 at 07:13

1 Answers1

6

This will solve your problem

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

list_center = [(1,2,3),(-4,-5,6), (5,5,6)]
list_radius = [1,2,1]

def plt_sphere(list_center, list_radius):
  for c, r in zip(list_center, list_radius):
    ax = fig.add_subplot(projection='3d')
    
    # draw sphere
    u, v = np.mgrid[0:2*np.pi:50j, 0:np.pi:50j]
    x = r*np.cos(u)*np.sin(v)
    y = r*np.sin(u)*np.sin(v)
    z = r*np.cos(v)

    ax.plot_surface(x-c[0], y-c[1], z-c[2], color=np.random.choice(['g','b']), alpha=0.5*np.random.random()+0.5)
fig = plt.figure()
plt_sphere(list_center, list_radius) 

surface plot

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Lawhatre
  • 1,302
  • 2
  • 10
  • 28