making 3D graph by limiting colors, define colors based on the values of z

I want to make a 3D graph, but I need only three colors according to the value of z. if z<1 gray if z>=1 red if z>1 white I was wondering if anyone help me.
x=(-.9:.1:.9)'; y=(.05:.05:.4); z=[66.4447 67.4929 68.6639 69.9560 71.3698 72.9075 74.5732 76.3727 35.4024 35.9272 36.4919 37.0986 37.7492 38.4462 39.1923 39.9908 24.5557 24.9086 25.2818 25.6764 26.0937 26.5350 27.0021 27.4965 18.7372 18.9946 19.2640 19.5461 19.8416 20.1513 20.4761 20.8170 14.9525 15.1428 15.3405 15.5460 15.7597 15.9820 16.2134 16.4545 12.2225 12.3602 12.5024 12.6491 12.8007 12.9573 13.1193 13.2867 10.1325 10.2269 10.3237 10.4230 10.5249 10.6294 10.7367 10.8468 8.4726 8.5306 8.5896 8.6497 8.7108 8.7731 8.8365 8.9011 7.1211 7.1479 7.1750 7.2024 7.2300 7.2579 7.2861 7.3145 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 5.0565 5.0331 5.0100 4.9871 4.9645 4.9421 4.9200 4.8981 4.2534 4.2094 4.1664 4.1246 4.0837 4.0439 4.0049 3.9669 3.5642 3.5018 3.4419 3.3845 3.3294 3.2765 3.2255 3.1765 2.9697 2.8905 2.8162 2.7464 2.6808 2.6189 2.5604 2.5052 2.4553 2.3603 2.2737 2.1946 2.1220 2.0551 1.9934 1.9363 2.0087 1.8979 1.8010 1.7156 1.6397 1.5721 1.5113 1.4566 1.6173 1.4894 1.3842 1.2961 1.2215 1.1575 1.1022 1.0539 1.2626 1.1151 1.0052 0.9204 0.8532 0.7988 0.7541 0.7168 0.8975 0.7324 0.6316 0.5643 0.5165 0.4812 0.4543 0.4334 ];
All the best

 Accepted Answer

%Your data, I made some assumptions about its shape
x=(-.9:.1:.9)';
y=(.05:.05:.4);
z=[66.4447 67.4929 68.6639 69.9560 71.3698 72.9075 74.5732 76.3727 35.4024 35.9272 36.4919 37.0986 37.7492 38.4462 39.1923 39.9908 24.5557 24.9086 25.2818 25.6764 26.0937 26.5350 27.0021 27.4965 18.7372 18.9946 19.2640 19.5461 19.8416 20.1513 20.4761 20.8170 14.9525 15.1428 15.3405 15.5460 15.7597 15.9820 16.2134 16.4545 12.2225 12.3602 12.5024 12.6491 12.8007 12.9573 13.1193 13.2867 10.1325 10.2269 10.3237 10.4230 10.5249 10.6294 10.7367 10.8468 8.4726 8.5306 8.5896 8.6497 8.7108 8.7731 8.8365 8.9011 7.1211 7.1479 7.1750 7.2024 7.2300 7.2579 7.2861 7.3145 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 5.0565 5.0331 5.0100 4.9871 4.9645 4.9421 4.9200 4.8981 4.2534 4.2094 4.1664 4.1246 4.0837 4.0439 4.0049 3.9669 3.5642 3.5018 3.4419 3.3845 3.3294 3.2765 3.2255 3.1765 2.9697 2.8905 2.8162 2.7464 2.6808 2.6189 2.5604 2.5052 2.4553 2.3603 2.2737 2.1946 2.1220 2.0551 1.9934 1.9363 2.0087 1.8979 1.8010 1.7156 1.6397 1.5721 1.5113 1.4566 1.6173 1.4894 1.3842 1.2961 1.2215 1.1575 1.1022 1.0539 1.2626 1.1151 1.0052 0.9204 0.8532 0.7988 0.7541 0.7168 0.8975 0.7324 0.6316 0.5643 0.5165 0.4812 0.4543 0.4334 ];
%gridded data
[xx yy] = meshgrid(x,y);
zz = reshape(z,size(xx));
%surf it!
h = surf(xx,yy,zz);
%build the index and colors
idx = zeros(size(zz));
idx(zz<1) = 1; %gray
idx(zz==1) = 2; %red
idx(~idx) = 3; %white
cdata = [.5 .5 .5;... %grayish
1 0 0;... %red
1 1 1]; %white
%set it
colormap(cdata)
set(h,'cdata',idx)
It appears you have no values exactly equal to one and only a few below it but this should do what you want!

11 Comments

thank you very much Sean
if we have x,y,z and V and we use slice(x,y,z,V) to make a volume, then how can we do this?
the exact same way I did, as far as the coloration is concerned. Now how do you get the the 3d volumetric data is up to you. Right now you only have 2d data.
Thank you, I tried to do that by I got this error:
??? Error using ==> set
CData must be an M-by-N matrix or M-by-N-by-3 array
I got the data as follows
x: 19*19*11
y: 19*19*11
z:19*19*11
v:19*19*11
xslice=[.3,.8];yslice =.5;zslice = [3,5,7];
h=slice(x,y,z,v,xslice,yslice,zslice);
Use
idx = zeros(size(zz),'uint8');
and subtract 1 from each of the indices that Sean shows -- e.g.,
idx(zz=1) = 1; %red
I got the same Error message, this is my code
%% gridded data
m=2:12;
p=(-.9:.1:.9);
a=(.05:.05:.95);
[alpha,phi,AgggLevel] = meshgrid(a,p,m);
%% slice them
load ARallM
xd=[.25,.55,.75,.95];yd =[-.5,0,.5,.9];zd = [2,5,7,10];
h=slice(alpha,phi,AgggLevel,v,xd,yd,zd);
%axis label and title
xlabel('\theta,autoregressive parameter'), ylabel('\alpha,smoothing parameter'), zlabel('m, Aggregation level')
title('MSE(BA)/MSE(AA)')
%% axis limits
set(gca,'XTick',.05:.1:.95)
set(gca,'YTick',-.9:.2:.9)
set(gca,'ZTick',2:12)
%%
zz=v;
%build the index and colors
idx = zeros(size(zz),'uint8');
idx(zz<1) = 1; %gray
idx(zz==1) = 2; %red
idx(~idx) = 3; %white
cdata = [.5 .5 .5;...%gray
1 0 0;...%red
1 1 1;... %
]; %white
%set it
colormap(cdata)
set(h,'cdata',idx)
I changed also
idx(zz<1) = 0; %gray
idx(zz==1) = 1; %red
idx(~idx) = 2; %white
but the same error
what does:
size(idx)
return? It should be 2d. 3d with the third dimension being 3.
finally I got my desired graph, but still there is an error
??? Error using ==> set
CData must be an M-by-N matrix or M-by-N-by-3 array

Sign in to comment.

More Answers (1)

There is no point in doing the volume slicing if you are going to set the color according to z.
Look at the example graph at http://www.mathworks.com/help/techdoc/ref/slice.html . With the color scheme you have indicated, everything above the z=1 level should be white; and all of the z planes drawn should be completely white (because your zd are all greater than 1); and there would be a line on the x and y planes where they cut z=1; and everything below that would be gray.
When I say "everything", I mean no circular blobs like in the example graph: it would just be a bunch of surfaces painted solid white or solid white adjacent to solid gray (and maybe a red line between those.)
I do know what is wrong with your current code, but it cannot be fixed until you rethink what you are doing.

5 Comments

I want to set colors according to v, not z.
cdata = [.5 .5 .5;... %grayish
1 0 0;... %red
1 1 1]; %white
%set it
colormap(cdata)
for K = 1 : length(h)
thiscdata = get(h, 'CData');
idx = zeros(size(thiscdata),'uint8');
idx(thiscdata < 1) = 0;
idx(thiscdata == 1) = 1;
idx(thiscdata > 1) = 2;
set(h,'CData',idx);
end
I got this error:
??? Undefined function or method 'lt' for input arguments of type 'cell'.
sorry, should be
thiscdata = get(h(K), 'CData');
and correspondingly,
set(h(K), 'CData', idx);
Thank you but there is a warning and it does not show the graph
Warning: CData must be double or single unless it is used only as a texture
data

Sign in to comment.

Categories

Find more on 2-D and 3-D 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!