Plotting spheres color according to a given value
33 views (last 30 days)
Show older comments
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,
0 Comments
Accepted Answer
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;
3 Comments
More Answers (3)
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;
0 Comments
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.
0 Comments
Francisco
on 6 Sep 2011
1 Comment
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.
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!