Colormap with both positive and negative values

130 ビュー (過去 30 日間)
Anders
Anders 2013 年 7 月 7 日
コメント済み: Walter Roberson 2017 年 11 月 9 日
Hi all,
I have a very simple issue, but the solution has been escaping me nonetheless.
I have a big matrix of correlations: some are negative and some are positive. I would like to plot them with a red-green colormap (like the "colormap(redgreencmap)" for "imagesc"). In this map, a correlation of -1 should be bright red, a correlation of +1 bright green and a correlation of close to zero black.
However, it seems like MATLAB can only handle matrices of values between [0,1] for colormap plotting.
This should be pretty simple, but I cannot figure out the solution. Does anyone have any suggestions?
Thanks a lot,
Anders

採用された回答

Image Analyst
Image Analyst 2013 年 7 月 7 日
編集済み: Image Analyst 2013 年 7 月 8 日
Try this:
% Create sample data:
correlations = peaks(300);
minValue = min(correlations(:));
maxValue = max(correlations(:));
% Scale the data to between -1 and +1.
correlations = (correlations-minValue) * 2 / (maxValue - minValue) - 1;
% Display - will use some weird color map to start with.
imagesc(correlations);
colorbar
% Create colormap that is green for negative, red for positive,
% and a chunk inthe middle that is black.
greenColorMap = [zeros(1, 132), linspace(0, 1, 124)];
redColorMap = [linspace(1, 0, 124), zeros(1, 132)];
colorMap = [redColorMap; greenColorMap; zeros(1, 256)]';
% Apply the colormap.
colormap(colorMap);
  3 件のコメント
Alexandria
Alexandria 2014 年 8 月 20 日
How would you go about changing the color from red/black/green, to say blue/black/yellow?
Nicolás Casaballe
Nicolás Casaballe 2016 年 3 月 14 日
If you are still after this, you can modify the line that defines the colorMap matrix. Its columns represent the RGB ratios of the colors that are later used to represent the image values.
Without changing too much of the code above (but the names are going to be quite misleading), the line
colorMap = [redColorMap; redColorMap; greenColorMap]'; % Actually not red and green
would build a blue/black/yellow colormap of sorts. Choosing better names is advisable (for instance redCol, greenCol and blueCol to make the columns).

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 7 月 7 日
The colormap should always be the RGB values to use, not the data values that are to be mapped into RGB values.
Create your colormap with bright red in the lower-numbered rows, black in the middle rows, and bright green in the last rows, and then imagesc() your data that is in the range -1 to +1
  2 件のコメント
Jason McCune-Sanders
Jason McCune-Sanders 2017 年 11 月 9 日
How would one easily change the shading of the map, i.e. add more colors or reduce the black zone in the middle? Thanks!
Walter Roberson
Walter Roberson 2017 年 11 月 9 日

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by