Rescale points plotted using scatter3 based on their distance from the origin

I wanted to know if its possible to rescale the markersizes used in scatter3 based on the marker's distance from the origin (0,0,0). I would like to make it so that if a point is closer to center then it is larger than the next point closests to it. Basically, I am hoping to have the largest points closer to the center and the smallest points farther away.
Here is the set of points that I am trying to rescale their markers. the biggest marker should be at (0,0,0) and then the rest should get smaller as they moved away from the center.
A = [0 0 0
0.00977495601952172 0.0129188554738323 0.999868768093125
-0.566794094824837 -0.823750570492204 0.0133959578031223
0.0279587435128966 0.0380588867245362 1.99938731654588
0.830388266617646 0.583999369869120 0.978401571089338
-1.07826433834531 -1.64452537544960 0.267810795303313
0.168715496407312 0.263085998125572 2.96351922435162
-0.791458202545459 0.611268411797120 0.998070417818667
-1.41124221175034 -0.289407593850698 0.0506110237693017
1.69942938355116 1.04462646221878 1.15892918358242
-1.55216375501531 -2.44987966243271 0.623934110066297
0.188310075544404 0.501770043093754 3.93441879647330
-1.44603145623221 1.35107113546187 1.15371676572365
-2.35391403973316 0.0183196401577155 0.179737992978120
2.59879677816763 1.38804628951095 1.42948622326796
-1.92629974765512 -3.28302931738291 1.03122259685150
0.190461468181228 0.731318108759712 4.90771374511875
-2.01837467713375 2.14014570650778 1.37684130228734
-3.30531517848174 0.217844651708771 0.414313445558867
3.50709118523837 1.65551615551852 1.75113998255253
-2.23204460424917 -4.13488361866807 1.45650407075820
0.193343935566412 0.934427321909631 5.88686559182864
-2.51244231909718 2.97180232130260 1.63030617210704
-4.25650570961388 0.357419056329789 0.689550723301627
4.41845060685608 1.87363023778730 2.10021053665969];

 Accepted Answer

Yes. Firstly get distances from the origin:
d = sqrt(sum(A.^2,2));
If you have the machine learning & statistics toolbox, you might prefer to use the pdist2 function. Next you need to define an equation where size decreases for increasing values of d, obviously you can define that how you like the following is just an example of one way you could do it:
sz = 5*(max(d) - d + 1) % some function do describe size w.r.t distance from O.
Then its just a case of inputting that into scatter3
scatter3(A(:,1),A(:,2),A(:,3),sz,'fill')

5 Comments

Hi, thanks for your suggestion. I just have a question about the documentation for scatter3. When it discusses resizing markers it uses uses repmat(), but I could not figure out how to get my s vector to be the same size as the input data?
x = A(:, 1);
y = A(:, 2);
z = A(:, 3);
s = repmat([3,2,1], numel(x),1);
scatter3(x, y, z, s, 'filled', 'k');
Are referring to the following example?
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
S = repmat([100,50,5],numel(X),1);
s = S(:);
figure
scatter3(x,y,z,s)
If so you are confusing x and X.
yes that was the example I was referring to. Thanks for the clarification, i just have one last question how would I recreate the different colors example on that same page with the data points I have?
s = 2*(max(d) - d + 1);
C = repmat([1,2,3],numel(x),1);
c = C(:,1);
scatter3(x, y, z, s,c);
You could do it as follows
scatter3(x,y,z,sz,d,'filled')
If you specifically want three groups of color based on the distance, d, you can modify the colormap:
colormap([1 0 0; 0 1 0; 0 0 1])
This is exactly what I needed thank you for all your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!