Create a surface plot with colors associated to value on cell

12 Ansichten (letzte 30 Tage)
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

Dr. Seis
Dr. Seis am 6 Okt. 2011
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 Kommentare
Igor de Britto
Igor de Britto am 11 Okt. 2011
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.
Dr. Seis
Dr. Seis am 14 Okt. 2011
Does "grid on" work? If so, you may have to play with the settings for "grid"

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
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
Fangjun Jiang
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
Igor de Britto am 14 Okt. 2011
@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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Orange finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by