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!