Create a surface plot with colors associated to value on cell
Show older comments
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?
Accepted Answer
More Answers (1)
Fangjun Jiang
on 6 Oct 2011
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 Comments
Igor de Britto
on 10 Oct 2011
Teja Muppirala
on 10 Oct 2011
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
on 10 Oct 2011
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
on 10 Oct 2011
@Teja, Thank you, Teja! I learned axis ij today!
Igor de Britto
on 11 Oct 2011
Igor de Britto
on 11 Oct 2011
Igor de Britto
on 11 Oct 2011
Fangjun Jiang
on 11 Oct 2011
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
on 14 Oct 2011
Categories
Find more on Orange 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!