Change color of different spheres

42 次查看(过去 30 天)
Bob Whiley
Bob Whiley 2015-4-20
I have the following code to plot a sphere
[x y z] = sphere; a=[loc ;radius]; a = transpose(a); surf(x*a(1,4)+a(1,1),y*a(1,4)+a(1,2),z*a(1,4)+a(1,3), 'FaceColor', 'interp') %daspect([1 1 1]) %view(30,10)
colormap(color) shading interp
with loc and radius as variable inputs. I am trying to plot multiple spheres with different colors each. However, when I run this code with multiple spheres using hold on, the only color I get is whatever last color input I put in. How can I edit my code so each sphere has its own color?
loc could be something like [1, 1, 1], radius can be something like 2 and color like [1 1 0] Thanks

回答(2 个)

pfb
pfb 2015-4-20
编辑:pfb 2015-4-20
Where exactly are you specifying the sphere color?
Because it seems to me that in the above command you're not.
You should go (e.g.)
surf(x*a(1,4)+a(1,1),y*a(1,4)+a(1,2),z*a(1,4)+a(1,3), 'FaceColor', [1 0 0]);
You might consider adding a light
light

Star Strider
Star Strider 2015-4-20
Set the 'FaceColor' property to the color you want. Borrowing the code from the sphere documentation and tweaking it:
[x,y,z] = sphere;
figure
hs1 = surf(x,y,z);
hold on
hs2 = surf(x+3,y-2,z); % centered at (3,-2,0)
hs3 = surf(x,y+1,z-3); % centered at (0,1,-3)
q1 = get(hs1);
set(hs1, 'FaceColor', [1 0 0])
set(hs2, 'FaceColor', [0 1 0])
set(hs3, 'FaceColor', [0 0 1])
axis equal
You may need to experiment to get the exact result you want.

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by