MATLAB Answers

How do I create a logarithmic scale colormap or colorbar?

1.658 views (last 30 days)
I need to color 'surf' plots on a log scale and subsequently displace the log-based colorbar.

Accepted Answer

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);
surf(X, Y, Z)
% Show the colorbar
% Set the log colobar
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.

More Answers (2)

lvn on 4 May 2018
Strange official answer, since it is clearly possible in Matlab:


Show 4 older comments
Sanat Kumar Tiwari
Sanat Kumar Tiwari on 31 Mar 2020
@Ivn, thanks for pointing out simple solution that is working fine. @Walter, your solution also gives similar results but the colorscales need some adjustment for best colorscheme.
It's surprising that official MATLAB answer is "feature not available" !
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.

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
% 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;
% set Ticks and TickLabels


Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by