Plotting spheres color according to a given value

27 次查看(过去 30 天)
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 个)

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