Plotting spheres color according to a given value

33 views (last 30 days)
Hi,
I have some spheres and I want to set their surface color according to their radii.
In my code I have the following matrix [xi,yi,zi,ri]; where xi, yi and zi are the XYZ coordinates of the center of the sphere and ri its radius. So, I need to relate the surface color of a given sphere to its radius in a colormap.
Does anyone know how to make it easily? It might be related to color mappings or RGB, but I am not very experienced on that stuff, so recommendations in this way would be very valuable.
Thanks,

Accepted Answer

Kelly Kearney
Kelly Kearney on 6 Sep 2011
Look more closely at the surf command; it accepts a fourth input to define the color of the surface, and only falls back on the z-data if you omit that input:
center = [...
0 0 0
1 1 1
2 1 1];
r = [1 1 0.5];
d = [1 0.5 0.3];
figure;
axes;
hold on;
[xu,yu,zu] = sphere;
for ii = 1:size(center)
x = xu*r(ii) + center(ii,1);
y = yu*r(ii) + center(ii,2);
z = zu*r(ii) + center(ii,3);
c = ones(size(z))*d(ii);
surf(x,y,z,c);
end
view(3);
axis equal;

More Answers (3)

Fangjun Jiang
Fangjun Jiang on 5 Sep 2011
May not be exactly what you want. Just for your reference.
data=[1,2,3,1;
2,3,4,.5
4,5,6,.3];
h=axes;hold on;
for k=1:size(data,1); [x,y,z]=ellipsoid(data(k,1),data(k,2),data(k,3),data(k,4),data(k,4),data(k,4));
surf(h,x,y,z);
end
view(3);grid on;

Walter Roberson
Walter Roberson on 6 Sep 2011
scatter3() can do that for you. The size parameter is the size of the sphere to draw at each x/y/z location; if you make the color parameter to be a vector the same as the size parameter, then the color would vary with the size.

Francisco
Francisco on 6 Sep 2011
Thank you guys but still need some help on this. I will try to specify exactly what I need:
Let's say we three spheres: A(0,0,0); B(1,1,1) and C(2,1,1); their radii are RA(1), RB(1) and RC(0.5). I want to represent a scalar value on each sphere, for example, density (DA(1), DB(0.5) and DC(0.3)).
Then, every sphere will be colored in just one color related to the value of the density. In this case, the surface of sphere A will be red (maximum value is 1) and the surface of sphere C will be colored in blue (because its the minimum).
- surf plots de value of z on the sphere, then different colors may appear on the surface
- scatter only represents circles of same radius, which is not my case
Thanks again,
  1 Comment
Walter Roberson
Walter Roberson on 6 Sep 2011
I suggest you re-read the scatter3() documentation, which says
S determines the area of each marker (specified in points^2). S can be a vector the same length as X, Y, and Z or a scalar. If S is a scalar, MATLAB draws all the markers the same size.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!