How do I create a logarithmic scale colormap or colorbar?

1,915 views (last 30 days)
MathWorks Support Team
MathWorks Support Team on 6 Jul 2012
Answered: Jakob Weis on 11 Apr 2022
I need to color 'surf' plots on a log scale and subsequently displace the log-based colorbar.

Accepted Answer

lvn
lvn on 4 May 2018
Edited: lvn on 17 Sep 2021
  7 Comments
Walter Roberson
Walter Roberson on 31 Mar 2020
You can tell by the wording of the official answer that it was written for an older version of MATLAB.

Sign in to comment.

More Answers (3)

Berthold Reisz
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)

MathWorks Support Team
MathWorks Support Team on 14 May 2020
Edited: MathWorks Support Team on 14 May 2020
Please follow the steps below to create a log colobar in 'surf' plot:
% Plot the surface plot
[X, Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
figure;
surf(X, Y, Z)
% Show the colorbar
colorbar
% Set the log colobar
set(gca,'ColorScale','log')
Note: This feature was introduced in MATLAB R2018a.
For more information, refer to the following documentation page, "Axes Properties":
  1 Comment
Samira Daneshgar
Samira Daneshgar on 12 Jan 2017
Hi, I would like to know how can I apply the same thing for the contourf. Thanks,

Sign in to comment.


Jakob Weis
Jakob Weis on 11 Apr 2022
Digging this oldie up again: As mentioned by Samira, the logarithmic ColorScale option does not seem to be working for contour and contourf. It looks like it's having trouble getting the color levels correct. Sure, an easy workaround is to plot the log10 and manually change the colorbar ticklabels but that doesn't look particularly nice. It'd be great if this got fixed in a future release.
Here's a MWE visualising the problem:
C = 10.^repmat(linspace(-5,5,1000),[1000,1]);
figure(1);clf
tiledlayout(2,2)
nexttile(1)
contourf(log10(C),50,'LineStyle','none')
title('contourf')
clim([-5 5])
cb1 = colorbar;
cb1.Label.String = 'log10(C)';
set(gca,'XTick',[],'YTick',[])
nexttile(3)
contourf(C,50,'LineStyle','none')
set(gca,'ColorScale','log')
clim([10^-5 10^5])
cb2 = colorbar;
cb2.Label.String = 'C';
set(gca,'XTick',[],'YTick',[])
nexttile(2)
p1 = pcolor(log10(C));
p1.EdgeColor = 'none';
title('pcolor')
clim([-5 5])
cb3 = colorbar;
cb3.Label.String = 'log10(C)';
set(gca,'XTick',[],'YTick',[])
nexttile(4)
p2 = pcolor(C);
set(gca,'ColorScale','log')
p2.EdgeColor = 'none';
clim([10^-5 10^5])
cb4 = colorbar;
cb4.Label.String = 'C';
set(gca,'XTick',[],'YTick',[])

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!