Create a surface plot with colors associated to value on cell
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (1)
Fangjun Jiang
am 6 Okt. 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 Kommentare
Igor de Britto
am 10 Okt. 2011
Teja Muppirala
am 10 Okt. 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
am 10 Okt. 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
am 10 Okt. 2011
@Teja, Thank you, Teja! I learned axis ij today!
Igor de Britto
am 11 Okt. 2011
Igor de Britto
am 11 Okt. 2011
Igor de Britto
am 11 Okt. 2011
Fangjun Jiang
am 11 Okt. 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
am 14 Okt. 2011
Kategorien
Mehr zu Introduction to Installation and Licensing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!