필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 12월 5일
%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
Sean de Wolski
Sean de Wolski 2011년 12월 6일
what does:
size(idx)
return? It should be 2d. 3d with the third dimension being 3.
bahman rostami tabar
bahman rostami tabar 2011년 12월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 6일
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
Walter Roberson
Walter Roberson 2011년 12월 6일
sorry, should be
thiscdata = get(h(K), 'CData');
and correspondingly,
set(h(K), 'CData', idx);
bahman rostami tabar
bahman rostami tabar 2011년 12월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by