File Exchange

image thumbnail


version 1.5 (2.89 KB) by

SCATTER3SPH Plots a 3d scatter plot with 3D spheres of different size and color



View License

SCATTER3SPH is like scatter3 only drawing spheres, instead of flat circles. It can represent three quantities as: height (Z), color and size for each combination of two variables (X and Y). The spheres will look 'spherical' no matter the axis scaling.
SCATTER3SPH(X,Y,Z) draws the spheres at coordinates in vectors X, Y and Z, with the default size and color. The three vectors have to be of the same length.

SCATTER3SPH(X,Y,Z,'size',S) draws the spheres with sizes S. If length(S)= 1 the same size is used for all spheres.

SCATTER3SPH(X,Y,Z,'color',C) draws the spheres with colors specified in a N-by-3 matrix C as RGB values.

SCATTER3SPH(X,Y,Z,'transp',T) applies transparency level 'T' to the spheres T= 0 => transparent, T= 1 => opaque.

Comments and Ratings (9)

Akshay Gore

Akshay Gore (view profile)

I uploaded a new version that does not have the problem mentioned by DBS adn Allen (I think). Spheres will always look spherical, independently of the axis scaling.

DBS: Just don't use different scales in the axis. This effect cannot be avoided. See D'Errico advise below.


DBS (view profile)

If you use different scales on the X,Y,Z axis, the balls will be flattened as plates... so it doesn't work for my case

Matteo Niccoli

Very nice of you to point to BUBBLEPLOT3 Francisco. Very much in line wit the spirit of this community.

Actually, there is another function in File Exchange which does a better job than mine: BUBBLEPLOT3 by Peter Bodin. Check it out.

John D'Errico

John D'Errico (view profile)

I saw Allen's comment, and I had to respond here. The "change" in shape is not a reflection on this code, but of the auto-scaling of MATLAB when it chooses the plot axes if your variables have different units.

If you wish to avoid that, then there are several things you can do. On 3-d plots that will be rotated, you might choose to execute the command

axis vis3d

after the scatter3ph call. This makes rotation more smooth anyway, and it prevents other problems in the rotation.

To enforce the sphere to look truly spherical from any orientation instead of ellipsoidal, then execute the command

axis equal

after the call to scatter3ph. Since both of these axis commands have other side affects, I am not recommending to the author to include them in the code. They are best left to the discretion of the user to apply as they wish or not.

In my opinion, this code has good help, an H1 line, error checks. I found only one glitch, a minor one at that. A standard paradigm in the MATLAB interface is that when you use property/value pairs to provide input parameters, here 'color','size', that you should allow the user to shorten those parameter names as long as the shortening is unambiguous. Thus we should be able to specify 'size' as any of 'siz', 'si', or 's'. In addition, caps should be ignored. So it should be acceptable to specify 'SIZE', or 'Size', or even 'SiZe' if the user chooses to do so. Since insensitivity to case takes nothing more than the use of the lower function in your code, that is a trivial fix. And there are several easy ways to test for a shortened property name. strmatch or strcmp or strcmpi all let you do it very efficiently. I have even seen it done in the switch statement itself, forming the case sub-statement as perhaps

case {'s' 'si' 'siz' 'size'}

My rating of 4 for this matter may seem too much of a drop for a problem that does not affect the usability of the code, but it makes sense to be consistent in your interface with the many other tools that MATLAB supplies. This way it is easy for others to use your code. I'll watch for updates as always, and will be happy to raise my rating to a 5 if this behavior is repaired.


Allen (view profile)

Spheres change shape with change in view orientation.

Rob Campbell

Rob Campbell (view profile)

Nice. I have used plot similar to this in the past and found them useful. Never got around to writing a separate function for the job, however.



Change demo figure


Added transparency as optional argument


Make the spheres always look spherical independently of axis scaling.


Heeding John D'Errico comments, now parameter names can be abbreviated (to 3 letters) and their case is irrelevant.


Change figure and description to better describe the utility of the function

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video