Plotting spheres color according to a given value

27 vues (au cours des 30 derniers jours)
Francisco
Francisco le 5 Sep 2011
Commenté : Raj Kishor le 8 Nov 2018
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,

Réponse acceptée

Kelly Kearney
Kelly Kearney le 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;
  3 commentaires
Francisco
Francisco le 8 Sep 2011
Thanks a lot! It works perfectly!
Raj Kishor
Raj Kishor le 8 Nov 2018
How the color is deciding based on d value. what is the rgb values?

Connectez-vous pour commenter.

Plus de réponses (3)

Fangjun Jiang
Fangjun Jiang le 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 le 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 le 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 commentaire
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by