Filter löschen
Filter löschen

Change color of different spheres

41 Ansichten (letzte 30 Tage)
Bob Whiley
Bob Whiley am 20 Apr. 2015
Beantwortet: Star Strider am 20 Apr. 2015
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

Antworten (2)

pfb
pfb am 20 Apr. 2015
Bearbeitet: pfb am 20 Apr. 2015
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 am 20 Apr. 2015
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.

Community Treasure Hunt

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

Start Hunting!

Translated by