# Multiple spheres from imported data

I have the centers, radius, and xyz-coordinates of say 200 spheres that I'd like to plot in a box. I know how to do one sphere using this information and Graphics3D, but do not know how to efficiently draw all 200.

How can I do it?

• In principle, you will want to Map the Sphere command over the list of centers, radii, etc. Can you show us the exact format of your data? Also, what do you mean by xyz coordinated? A sphere is already uniquely determined by its center and radius. Mar 11, 2016 at 17:54
• Thanks for your response. I get my data from running a post-processing script in MATLAB - so the format is flexible. I plan on importing four different arrays from MATLAB into MATHEMATICA: Arry1=x coordinates of the center, Arry2=y coordinates of the center, Arry3=z coordinates of the center, Arry4=radii. The mapping is exactly my problem here. How do i pass on the information to Graphics3D? Mar 11, 2016 at 18:27

Update:

After you clarified the format of your data that is imported in four arrays, here is a modified approach. Again, I will generate some fake data to play with:

array1 = RandomReal[{-10, 10}, 10]; (* = imported x coords *)
array2 = RandomReal[{-10, 10}, 10]; (* = imported y coords *)
array3 = RandomReal[{-10, 10}, 10]; (* = imported z coords *)
array4 = RandomReal[2, 10];         (* = imported radii    *)


You can then generate your Sphere expressions using MapThread (the cleaner, more readable choice, as suggested by J.M. below):

spheres = MapThread[Sphere[{#1, #2, #3}, #4] &, {array1, array2, array3, array4}];


or alternatively by explicit application, which was my first attempt:

spheres = Sphere[{#1, #2, #3}, #4] & @@@ Transpose[{array1, array2, array3, array4}];


Generate some play data for 10 random spheres in the form {{centerX, centerY, centerZ}, radius}:

spheredata = Transpose@{RandomReal[{-10, 10}, {10, 3}], RandomReal[2, 10]}


The generate Sphere objects from each of them and feed them to Graphics3D:

Graphics3D[
Sphere @@@ spheredata
]


• Additionally if all the spheres had equal radii, then Sphere[(* point list *), (* radius *)] suffices. Mar 11, 2016 at 18:16
• OK, got it by manipulating your example. Thanks for your help. Mar 11, 2016 at 18:37
• @user147813 I have just posted a modification of my answer for your format, but if you got it to work on your own already already, even better! Glad to help! Mar 11, 2016 at 18:39
• I'd have done it as spheres = MapThread[Sphere[{#1, #2, #3}, #4] &, {array1, array2, array3, array4}]; myself. Mar 11, 2016 at 18:45
• @J.M. Yes, that's cleaner. I'll add it to my answer. Mar 11, 2016 at 18:49