Plotting spheres color according to a given value

조회 수: 27 (최근 30일)
Francisco
Francisco 2011년 9월 5일
댓글: Raj Kishor 2018년 11월 8일
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,

채택된 답변

Kelly Kearney
Kelly Kearney 2011년 9월 6일
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
Francisco
Francisco 2011년 9월 8일
Thanks a lot! It works perfectly!
Raj Kishor
Raj Kishor 2018년 11월 8일
How the color is deciding based on d value. what is the rgb values?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 9월 5일
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 2011년 9월 6일
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 2011년 9월 6일
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
Walter Roberson
Walter Roberson 2011년 9월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by