Create a surface plot with colors associated to value on cell
이전 댓글 표시
I have a matrix with 3 possible values: 1, 0 and -1. They may contain any combination of those elements. I'd like to choose the colors for each value (like blue for 1, gray for 0 and red for -1).
When I try surface(matrix), it's not possible to differ: surface(ones(X)) surface(zeros(X)) surface(-ones(X))
Any idea on how to get on with it?
채택된 답변
추가 답변 (1개)
Fangjun Jiang
2011년 10월 6일
use pcolor()
Two notes. The last row and last column is not shown so you might want to pad NaNs. Second, pay attention to the image shown. The color on grid (x,y) corresponds to the value in a(x,y). Don't try to map the color with the position of matrix shown in the Command Window. For example:
a=[1 1 -1;0 0 1;-1 -1 1;-1 -1 1];
b=a;
b(end+1,:)=nan;
b(:,end+1)=nan;
pcolor(b);
댓글 수: 9
Igor de Britto
2011년 10월 10일
Teja Muppirala
2011년 10월 10일
Add these two lines at the end, and it should work like you expect:
axis ij %<--- Flips the image to make it look like the matrix
set(gca,'clim',[-1 1]) %<--- Makes the colors and values match
Fangjun Jiang
2011년 10월 10일
Yes, you are right. If you look at the help of colormap, that is supposed to be that way. You can add caxis([-1 1]) after the colormap() to indicate that you always want to specify -1 and 1 as the min and max value of your data, no matter what values are in your data. That will solve the problem.
Fangjun Jiang
2011년 10월 10일
@Teja, Thank you, Teja! I learned axis ij today!
Igor de Britto
2011년 10월 11일
Igor de Britto
2011년 10월 11일
Igor de Britto
2011년 10월 11일
Fangjun Jiang
2011년 10월 11일
I am not sure what's wrong with your code. But when I run the following, it is exactly as expected. The top 2 rows are red, the bottom one row is gray.
%%
cmap = [1,0,0;0.5,0.5,0.5;0,0,1]; % 1st row = red; 2nd = gray; 3rd = blue
%I have this matrix
A = [-1 -1 -1
-1 -1 -1
0 0 0];
a=A;a(end+1,:)=nan;a(:,end+1)=nan;
pcolor(a)
colormap(cmap);
axis ij; %<--- Flips the image to make it look like the matrix
caxis([-1 1]); %<--- Makes the colors and values match
Igor de Britto
2011년 10월 14일
카테고리
도움말 센터 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!