Colormap fixed middle value

조회 수: 50 (최근 30일)
ghastliness
ghastliness 2016년 9월 29일
댓글: Fizzle 2021년 11월 14일
Hej all, for a pcolor plot I have built the ratio of a treatment vs. a control (A./B) and want to plot the resulting pcolor plot with colors according to their value - In this case, 0-1 should always be in a gradient from blue to white, while everything above 1 (differing maxValues) should be in a gradient white to red. Is there a way to "fix" the colorbar so that the white color will always be displayed at value 1 of my data? (or can I use 2 colormaps for this somehow?)

채택된 답변

Brandon Eidson
Brandon Eidson 2016년 10월 4일
You can definitely create a custom color map in MATLAB. One way to do this is by defining the RGB values for your maximum, minimum, and indexed value (your value of "1" is what I am calling the indexed value). You can then create a vector representing RGB values ranging from the minimum to the indexed colors and from the indexed to the maximum colors.
Because the index value may not be the median value of the dataset, the vectors' sizes will need to be adjusted proportionally to where the index falls between the minimum and maximum values.
I have written a script that illustrates this workaround. See if you can apply it to your situation.
L = 10; %number of datapoints
data = 3.6*rand(L); % create example data set with values ranging from 0 to 3.6
indexValue = 1; % value for which to set a particular color
topColor = [1 0 0]; % color for maximum data value (red = [1 0 0])
indexColor = [1 1 1]; % color for indexed data value (white = [1 1 1])
bottomcolor = [0 0 1]; % color for minimum data value (blue = [0 0 1])
% Calculate where proportionally indexValue lies between minimum and
% maximum values
largest = max(max(data));
smallest = min(min(data));
index = L*abs(indexValue-smallest)/(largest-smallest);
% Create color map ranging from bottom color to index color
% Multipling number of points by 100 adds more resolution
customCMap1 = [linspace(bottomcolor(1),indexColor(1),100*index)',...
linspace(bottomcolor(2),indexColor(2),100*index)',...
linspace(bottomcolor(3),indexColor(3),100*index)'];
% Create color map ranging from index color to top color
% Multipling number of points by 100 adds more resolution
customCMap2 = [linspace(indexColor(1),topColor(1),100*(L-index))',...
linspace(indexColor(2),topColor(2),100*(L-index))',...
linspace(indexColor(3),topColor(3),100*(L-index))'];
customCMap = [customCMap1;customCMap2]; % Combine colormaps
colormap(customCMap)
psudo = pcolor(data);
colorbar
  댓글 수: 4
Charles Rice
Charles Rice 2019년 3월 25일
Instead of max(max(A)) you can also use max(A, [], 'all') or max(A(:)). The other methods work no matter the dimensions of your data.
Fizzle
Fizzle 2021년 11월 14일
Thank you Brandon, but I tried this method for a matrix containing negative numbers and it didn't exactly work for me.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Debashish Saha
Debashish Saha 2018년 7월 3일
Hi Brandon, does it also work for a vector containing negative values? In my case, it seems that 0 value is not at the exact location / point of white in the color bar. Should one take the absolute of the smallest value to negate the negative number in the vector while determining the index in your code? Thanks in advance. Cheers, DS

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by