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
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
2011-12-6
what does:
size(idx)
return? It should be 2d. 3d with the third dimension being 3.
更多回答(1 个)
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
2011-12-6
sorry, should be
thiscdata = get(h(K), 'CData');
and correspondingly,
set(h(K), 'CData', idx);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!