Change color of different spheres

조회 수: 8 (최근 30일)
Bob Whiley
Bob Whiley 2015년 4월 20일
답변: Star Strider 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에서 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