Unevenly Map Data to an RGB Map?

2 views (last 30 days)
Blenndrman
Blenndrman on 18 Sep 2020
Answered: DGM on 28 May 2022
So I have made an RGB style map that mimics Matlab's color map options (just a different flavor, for stylistic reasons), and I want it to scale to the bottom two thirds of my data. I want it to linearly scale to the bottom two thirds because the peaks are so high that a perfect linear match hides small-scale detail elsewhere in the figures. Any good tricks?
For context, I'm plotting with surfc and then:
load('rgbmap.mat'); %Homemade colormap stored here as variable "map"
colormap(map);
c = colorbar;
Thank you.
  1 Comment
Mohammad Sami
Mohammad Sami on 19 Sep 2020
The easiest way would be to edit the colour map in the figure GUI.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Sep 2020
When you use a colormap, the entry-number used always scales linearly between the upper and lower limit established on the color axes (caxis) .
One approach is thus to create a colormap that conceptually spans the entire range of your data, but you repeat colors in the upper 1/3 (or as appropriate). It is completely valid to use a colormap that goes something like
black
red/8
2*red/8
3*red/8
4*red/8
yellow
yellow
yellow
blue
If you are using a sufficiently old MATLAB on MS Windows then you are limited to 256 entries in a colormap; other operating systems and newer MATLAB for Windows permit 65536 entries.
Another approach is to do the color mapping yourself. You can, for example, discretize() the Z range to get bin numbers, and ind2rgb(binNumbers, your_color_map) to get an RGB image. You can then set that as the CData of the surf() -- though I find that texture mapping like this is often easier to accomplish using warp() instead of surf() .

More Answers (2)

Mohammad Sami
Mohammad Sami on 19 Sep 2020
You can use the colormapeditor GUI to interactively create the colour map.
https://www.mathworks.com/help/matlab/ref/colormapeditor.html

DGM
DGM on 28 May 2022
If you want to take your colormap and shift it toward one end or the other for some reason, you might be able to do something like this:
% say you have some colormap
cmap0 = parula(64);
% scale adjustment parameter (range is [-1 1])
% when stretchamt = 0, no change
% when stretchamt < 0, stretch bottom of table upward
% when stretchamt > 0, stretch top of table downward
stretchamt = 0.5;
% delinearize adjustment parameter to get gamma
% apply gamma adjustment to primary axis of the CT
% conditionally flip table so xnew is effectively point-symmetric about [0.5 0.5]
% this limits the slope of xnew and avoids overcompressing the table for g<1
n = size(cmap0,1);
x = linspace(0,1,n);
if stretchamt > 0
g = 1 - min(1-eps,stretchamt);
xnew = x.^(1/g);
cmapnew = flipud(interp1(x,flipud(cmap0),xnew,'pchip'));
else
g = 1/(1 + max(eps-1,stretchamt));
xnew = x.^g;
cmapnew = interp1(x,cmap0,xnew,'pchip');
end
% cmapnew is your new color table
cmapnew
cmapnew = 64×3
0.2422 0.1504 0.6603 0.2577 0.1815 0.7504 0.2703 0.2135 0.8340 0.2779 0.2527 0.8954 0.2812 0.2949 0.9368 0.2803 0.3360 0.9665 0.2737 0.3763 0.9866 0.2573 0.4175 0.9962 0.2210 0.4599 0.9973 0.1860 0.5010 0.9827
For sake of visualization, this is what the results look like for stretchamt equal to -0.6, -0.3, 0, 0.3, and 0.6 respectively

Community Treasure Hunt

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

Start Hunting!