How to fix a limit between 2 combined colormaps?

2 views (last 30 days)
My problems is that I'm trying to use two combined colorbars to get more color range in depicting my values. But if I define clim ([100 350]) w the average gets used as the splitting point between the 2 colormaps (meaning here 225). Now, I know that I can fix the colorbar manually by shifting it in the figures and saving it then, but this is very impractical since I'm trying to create many figures. Is there any good line of code, that could help me fix the limit directly?
My code:
clim([100 350])
colormap([flipud(m_colmap('blues')); colormap(slanCM(6))])
I would like to use the "blues'" colormap for values between 100 and 200, the other one for values between 200 and 350.
Any help would be greatly appreciated.

Accepted Answer

DGM
DGM on 3 Jul 2024
Edited: DGM on 3 Jul 2024
Get out of the habit of generating colormaps without specified lengths. If you use implicit lengths, you can easily wind up with the map lengths varying uncontrollably every time you run your code unless the figure is deleted (not just cleared).
You have two unequal intervals, so your two map segments need to have different lengths. You can either do that by generating them at that length.
% some fake data
x = 50:175;
Z = x + x.';
% plot it somehow
imagesc(Z)
% colormap breakpoints and lengths
breakpts = [100 200 350];
l1 = 256; % pick something
l2 = round(l1*(breakpts(3) - breakpts(2))/(breakpts(2) - breakpts(1)));
% generate and apply the colormaps
CT1 = m_colmap('blues',l1);
CT2 = slanCM('parula',l2);
colormap([flipud(CT1); CT2])
% set the colorbar/clim
clim(breakpts([1 end]))
colorbar
Alternatively, if you want the slopes of the two maps to be the same, you can generate the two maps to have the same length, and then trim the shorter map to length. Note now that 'blues' doesn't run all the way to white anymore.
% some fake data
x = 50:175;
Z = x + x.';
% plot it somehow
imagesc(Z)
% colormap breakpoints and lengths
breakpts = [100 200 350];
l0 = 512; % the length of the longer map segment
l1 = round(l0*(breakpts(2) - breakpts(1))/(breakpts(3) - breakpts(2)))
% generate and apply the colormaps
CT1 = m_colmap('blues',l0);
CT1 = CT1(1:l1,:); % trim the shorter map segment
CT2 = slanCM('parula',l0);
colormap([flipud(CT1); CT2])
% set the colorbar/clim
clim(breakpts([1 end]))
colorbar

More Answers (0)

Community Treasure Hunt

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

Start Hunting!