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?

 Accepted Answer

Check out "colormap" in the Matlab Help Navigator. Right under the "Description" section, they talk about creating your own color map. You would define your color map such that the RGB values result in the desired colors to be applied to your surface plot.
Use colormap if you want to choose your own colors...
a=[1 1 -1;0 0 1;-1 -1 1;-1 -1 1];
cmap = [1,0,0;0.5,0.5,0.5;0,0,1]; % 1st row = red; 2nd = gray; 3rd = blue
imagesc(a);
colormap(cmap);

4 Comments

Thanks for the reply. It works wonderfully whenever the matrix has the 3 values (1, 0 and -1). However, if that's not the case, there are some complications.
cmap = [1,0,0;0.5,0.5,0.5;0,0,1]; % 1st row = red; 2nd = gray; 3rd = blue
A = [1 0 0; 0 1 0; 0 0 1];
B = zeros(3);
C = [1 0 -1; 0 -1 1; -1 1 0];
figure
imagesc(A)
colormap(cmap);
%%>>> returns a surface where the "1"s are represented as blue pixels and "0"s are represented as red pixels, so the last part is not ok.
figure
imagesc(B)
colormap(cmap);
%%>>> returns a surface where the "0"s are represented as gray pixels, so that's ok.
figure
imagesc(C)
colormap(cmap);
%%>>> returns a surface where the "1"s are represented as blue pixels and "0"s are represented as gray pixels and "-1"s are represented as red pixels. Everything is great!
Any ideas on how to make them work properly (blue for "1", gray for "0", red for "-1") when the 3 values are not all necessairely present?
Thanks again!
Sorry about that!
Add "caxis" to the above, for example:
figure
imagesc(A)
colormap(cmap);
caxis([-1 1]);
This will force the colors to conform to the desired scale.
Thanks a lot, Elige. It works like a charm =) Do you know, however, how can I make the gridlines (something like I would get from a surface or pcolor plot) come out? I tried the caxis command on pcolor, but it does not work.
Does "grid on" work? If so, you may have to play with the settings for "grid"

Sign in to comment.

More Answers (1)

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

Thanks for the reply. It works wonderfully whenever the matrix has the 3 values (1, 0 and -1). However, if that's not the case, there are some complications.
cmap = [1,0,0;0.5,0.5,0.5;0,0,1]; % 1st row = red; 2nd = gray; 3rd = blue
A = [1 0 0; 0 1 0; 0 0 1];
a = A;
a(end+1,:)=nan;
a(:,end+1)=nan;
B = zeros(3);
b = B;
b(end+1,:)=nan;
b(:,end+1)=nan;
C = [1 0 -1; 0 -1 1; -1 1 0];
c = C;
c(end+1,:)=nan;
c(:,end+1)=nan;
figure
pcolor(a)
colormap(cmap);
%%>>> returns a surface where the "1"s are represented as blue pixels and "0"s are represented as red pixels, so the last part is not ok.
figure
pcolor(b)
colormap(cmap);
%%>>> returns a surface where the "0"s are represented as gray pixels, so that's ok.
figure
pcolor(c)
colormap(cmap);
%%>>> returns a surface where the "1"s are represented as red pixels and "0"s are represented as blue pixels and "-1"s are represented as gray pixels. Everything is misplaced.
Any ideas on how to make them work properly (blue for "1", gray for "0", red for "-1")?
Thanks again!
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
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.
@Teja, Thank you, Teja! I learned axis ij today!
@Teja thanks a lot. The axis ij is very useful (was trying to fix it, but it was a minor inconvenience!). However, I'm not having any luck with the set command you suggested.
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];
pcolor(A)
colormap(cmap);
axis ij; %<--- Flips the image to make it look like the matrix
set(gca,'clim',[-1 1]); %<--- Makes the colors and values match
I'm getting the upper part (-1) as gray and the lowest (0) as blue. I'm posting my whole code so that you may find something missing or messing the plot.
for a = 1:8
h = subplot(4,8,x(a)), pcolor(SIS_tab{a}) ;
xlim([1 Especies]); % limits for X axis
ylim([1 Especies]); % limits for Y axis
axis off; % removes the numbers - thinks of 'xticks []'. Better off like that?
colormap(cmap);
title('Species Interactions'); % title
axis ij; %<--- Flips the image to make it look like the matrix
set(h,'clim',[-1 1]); %<--- Makes the colors and values match
end
% the "a loop" means that the number of lines made of 1 or -1 is going to increase from 1 to 12,5%, 25%, 50%, 75% 87,5% and n-1 lines.
Also tried with "set(gca...", but didn't work, as it requires an specific object, I guess.
Any ideas?
@Fangjun thanks a lot for the reply. Tried the "caxis" aproach placing it just bellow the colormap line (in the same code shown above), but got the same result (gray for 0, blue for -1). It was just plain easier on Excel =P
Any idea on how to contour this problem? Thanks again!
@Fangjun Interestingly enough, the caxis command did work on Imagesc (suggested bellow) instead of pcolor. Is caxis supposed to work only on some plot tools? I'd appreciate to have the grid that comes with surface and pcolor plots. Thanks!
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
@Fangjun I managed to reinstall Matlab after getting a few more errors. Now the codes works just as expected!
@Fangjun, Elige and Teja: Can't really thank you enough! Things are looking pretty good now and you just made possible to make my masters graphics =). There are a few things to be done (like naming axis and the likes), but the crucial part was getting the plot collors right! Thanks a lot, people!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!