Heatmap log colour scale: caxis() doesn't map values correctly?
Show older comments
I have a 2D array with logarithmically distributed values from [1e-5, 1e5]. For various reasons, I want to change the colourbar with limits beyond this range. I do this using caxis, e.g. caxis([a,b]), where a and b are the desired exponents. However the resulting colourbar does not map correctly.
Example problem, starting with log-distributed values and desired limits of [1e-5, 1e5]:
% Create log values and generate heatmap
dim = 10;
x = logspace(-5,5,dim);
values = repmat(x,[dim,1]);
hm = heatmap(values);
% Make visually clearer
hm.GridVisible = 'off';
colormap default
ax1 = gca;
ax1.XDisplayLabels = nan(length(ax1.XDisplayData),1);
ax1.YDisplayLabels = nan(length(ax1.YDisplayData),1);
% Colour bar scaling - PROBLEM
set(gca,'ColorScaling','log') % Log scale
caxis([-5,5]) % Lower/Upper limits (exponents) (to change!)
ax2 = struct(gca);
cb = ax2.Colorbar;
cb.Ticks = [1e0,1e1,1e2,1e3,1e4,1e5]; % Example ticks to show problem...
After the limits are specified, the colour mapping becomes skewed. If you pause the code after the 'set' line, the caxis values give [-11.5, 11.5], not [-5, 5]. Therefore, if I desire a fixed colour range such as [1e-7, 1e7], I apparently need to fudge the values. What's going on? Does caxis() not define exponents in the way that it should?
Accepted Answer
More Answers (1)
Mathieu NOE
on 1 Dec 2021
hello
no mre problem my friend !

% Create log values and generate heatmap
dim = 10;
x = logspace(-5,5,dim);
values = repmat(x,[dim,1]);
hm = heatmap(values);
% Make visually clearer
hm.GridVisible = 'off';
ax1 = gca;
ax1.XDisplayLabels = nan(length(ax1.XDisplayData),1);
ax1.YDisplayLabels = nan(length(ax1.YDisplayData),1);
% Colour bar scaling
N = 256; % colorbar discrete values
cmap = colormap(jet(N)) ; %Create Colormap
set(gca,'ColorScaling','log') % Log scale
ax2 = struct(gca);
cbh = ax2.Colorbar;
tmp = logspace(min(log10(values),[],'all'), max(log10(values),[],'all'), dim+1);
cbh.Ticks = tmp ; %Create N ticks from min to max of Z array
cbh.TickLabels = num2cell(tmp) ; %Replace the labels of these N ticks with the numbers defined in "tmp"
1 Comment
Duncan Ingram
on 1 Dec 2021
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!