Plotting spheres of radius R
Asked Answered
O

1

5

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 ?

Overage answered 3/11, 2020 at 4:47 Comment(4)
Can you provide sample plot. Do you just want to plot a single sphere or 10 sphere? You may provide sample input and output.Appall
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)Overage
will scatter plot work for you? or you need surf plot ?Appall
Surf plot is desired first. In scatter plot how will you make the radius values as specified by the user ?Overage
A
7

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

Appall answered 3/11, 2020 at 7:32 Comment(15)
Yes this is useful but if I have a set of 20 coordinates , with x , y and z coordinates stored in each separate array , how to make the list_center and list_radius from each of the three arrays.Overage
Can I also rotate the image in python to see the 2D view of the above 3D surfaceOverage
You can try plt.xticks(rotation=) plt.yticks(rotation=)Appall
Can you provide sample input for "Yes this is useful but if I have a set of 20 coordinates , with x , y and z coordinates stored in each separate array , how to make the list_center and list_radius from each of the three arrays" This is confusing actually.Appall
Do you mean you have stored coordinate centers in 3 different arrays?Appall
Ya actually these coordinates and radius values have been provided to me by someone. Lets say I have these values of x y and z coordinates and the radius values stored in an array. Now how to extract those values from the array and put them in list_centerOverage
Yes one array for storing the x coordinate , one array for y coordinate and one array for z coordinate and one array for radiusOverage
I tried this plt.xticks(rotation=) plt.yticks(rotation=). But it is showing an error after = sign saying invalid syntax. I think there must be something after =Overage
list_centre= [(i,j,k) for i,j,k in zip(x_arr,y_arr,z_arr)] This will do the trick :)Appall
yes, you have to assign an angle value after = . Set it to a desired value.Appall
Yes Thanks but rotation effect using plt.xticks is showing an errorOverage
It is giving a list <a list of 9 Text yticklabel objects> . But i am not able to rotate the image yet.Overage
ax.view_init(elev=0, azim=10) play around with this. Also, put this belowplot_surface command. This will definately solve.Appall
This also didnt work. But I found out that by importing matplotlib notebook , it has an option to rotate the axis.Overage
Shouldn't the code read 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)? Otherwise the sign of my origin coordinates is wrong.Bleat

© 2022 - 2024 — McMap. All rights reserved.