Plotting a matrix using colormap and imagesc and modifying colors

55 ビュー (過去 30 日間)
Nicole
Nicole 2014 年 9 月 28 日
回答済み: Geoff Hayes 2014 年 9 月 28 日
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?

回答 (1 件)

Geoff Hayes
Geoff Hayes 2014 年 9 月 28 日
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?

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by