How do I set retain the colors of SCATTER3 or a SURFACE plot after I plot a MESH over it in MATLAB 7.6 (R2008a)?

7 views (last 30 days)
I have a SCATTER3 plot and when I plot a MESH on the same axis using 'hold on', the colors of the SCATTER3 plot changes. I want to retain these colors.
Reproduction Steps:
[x,y,z] = sphere(16);
X = [x(:)*.5 x(:)*.75 x(:)];
Y = [y(:)*.5 y(:)*.75 y(:)];
Z = [z(:)*.5 z(:)*.75 z(:)];
S = repmat([1 .75 .5]*10,prod(size(x)),1);
C = repmat([1 2 3],prod(size(x)),1);
hs = scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60)
hold on
[X,Y] = meshgrid(-1:.125:1);
Z = peaks(X,Y);
h1 = meshc(X,Y,Z);
axis([-3 3 -3 3 -10 5])
You will see that initial colors of the scatter plot was lost after the mesh was plotted.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
To retain the original color values for scatter plot after overlaying a mesh plot, use the 'CLim' property of the axes by setting it to the 'CLim' values after the scatter plot was made.
As an example:
[x,y,z] = sphere(16);
X = [x(:)*.5 x(:)*.75 x(:)];
Y = [y(:)*.5 y(:)*.75 y(:)];
Z = [z(:)*.5 z(:)*.75 z(:)];
S = repmat([1 .75 .5]*10,prod(size(x)),1);
C = repmat([1 2 3],prod(size(x)),1);
hs = scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60)
%Get the CLim values after plotting scatterplot
a = get(gca,'Clim')
hold on
[X,Y] = meshgrid(-1:.125:1);
Z = peaks(X,Y);
h1 = meshc(X,Y,Z);
axis([-3 3 -3 3 -10 5])
% Set the previous value of CLim as the Clim of axis
set(gca,'Clim',a);

More Answers (0)

Tags

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!