Colormap fixed middle value
31 次查看(过去 30 天)
显示 更早的评论
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?)
0 个评论
采纳的回答
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
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
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!