MATLAB Answers

1

How do I create a logarithmic scale colormap or colorbar?

I need to color SURF plots on a log scale and subsequently displace the log-based colorbar.

3 Answers

Answer by MathWorks Support Team on 17 May 2018
Edited by MathWorks Support Team on 17 May 2018
 Accepted Answer

The ability to create a logarithmic scale COLORMAP or COLORBAR is not available in MATLAB.
A fellow customer posted the following suggested workaround for this problem on our USENET newsgroup, comp.soft-sys.matlab (CSSM):
% D is your data
% Rescale data 1-64
d = log10(D);
mn = min(d(:));
rng = max(d(:))-mn;
d = 1+63*(d-mn)/rng; % Self scale data
image(d);
hC = colorbar;
L = [0.01 0.02 0.05 0.1 0.2 0.5 1 2 5 10 20 50 100 200 500 1000 2000 5000];
% Choose appropriate
% or somehow auto generate colorbar labels
l = 1+63*(log10(L)-mn)/rng; % Tick mark positions
set(hC,'Ytick',l,'YTicklabel',L);
Note that this method has not been fully tested by MathWorks. MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.

  1 Comment

Hi, I would like to know how can I apply the same thing for the contourf. Thanks,

Sign in to comment.


Answer by lvn
on 4 May 2018

Strange official answer, since it is clearly possible in Matlab:
set(gca,'ColorScale','log')

  5 Comments

Can confirm it was introduced in 2018a
Another approach for R2018a is:
cb = colorbar();
cb.Ruler.Scale = 'log';
cb.Ruler.MinorTick = 'on';
In theory this should also work in R2017b (and perhaps other releases), but in R2017b you will get an warning message,
Warning: Error updating ColorBar.
DataSpace or ColorSpace transform method failed
I get this error also in R2018a ...

Sign in to comment.


Answer by Berthold Reisz on 15 Mar 2019

Try the following:
% let A be your data
A = 100*rand(100,100);
% plot log10 of A
pcolor(log10(A))
% get the minimum and maximum value of A
c1 = min(min(A));
c2 = max(max(A));
% set limits for the caxis
caxis([log10(c1) log10(c2)]);
% preallocate Ticks and TickLabels
num_of_ticks = 5;
Ticks = zeros(1,num_of_ticks);
TickLabels = zeros(1,num_of_ticks);
% distribute Ticks and TickLabels
for n = 1:1:num_of_ticks
Ticks(n) = log10(round(c2)/num_of_ticks*n);
TickLabels(n) = round(c2)/num_of_ticks*n;
end
% set Ticks and TickLabels
colorbar('Ticks',Ticks,'TickLabels',TickLabels)

  0 Comments

Sign in to comment.