Plotting a matrix using colormap and imagesc and modifying colors

48 Ansichten (letzte 30 Tage)
Nicole
Nicole am 28 Sep. 2014
Beantwortet: Geoff Hayes am 28 Sep. 2014
Hi,
I am trying to change the colors of my plot but I am not sure how to do that. My code is:
A=zeros(5)
A(3,3)=R5
figure
colormap('default')
imagesc(A)
I would like 0 to equal green and 3 to equal black. can i do that with these commands or are their better options out there?

Antworten (1)

Geoff Hayes
Geoff Hayes am 28 Sep. 2014
Nicole - there are probably several ways to do this, so here is one. Define your own color map as
myColorMap = [0 1 0;
0 0 0];
where each row is an RGB vector that defines one colour (each real number must be between 0 and 1). Note that the first row corresponds to green since we have a one in the G (green) column and zeros elsewhere. The second row corresponds to black since a zero is present in the R (first), G (second), and B (third) columns.
If your A is defined to be
A =
0 0 0 0 0
0 0 0 0 0
0 0 3 0 0
0 0 0 0 0
0 0 0 0 0
then to display the centre element as black and the rest as green, we set the current color map
colormap(myColorMap);
and display your image
imagesc(A) % or image(A)
The catch with the above is how do you map other values (not zeros or threes) in A to different colors?

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by