Plotting powers of 10 on the colorbar

Hello,
Is there any way to have powers of 10 (like 1e5, 1e6, 1e7) to be plotted on the colorbar as tickmarks without resorting to using log10? I tried the following code.
col = colorbar('XTickLabel', {'10^{5}', '10^{6}', '10^{7}'}, 'XTick',1e10:1e11:1e12);
caxis([1e10 1e12])
but that just gives me a colorbar with the xticks repeated as shown in colorbar.jpg (attached).
I also tried taking the log10 of these values as shown by the following code and got the desired output as shown in colorbar_log.jpg (attached):
col = colorbar('XTickLabel', {'10^{5}', '10^{6}', '10^{7}'}, 'XTick',log10(1e5):1:log10(1e7));
caxis([log10(1e5) log10(1e7)])
I however was wondering whether the same output as in colorbar_log.jpg can be obtained without resorting to taking log10.
Thanks.

 Respuesta aceptada

These are 'Ruler' properties, and they are not in the colorbar documentation. (I had to go searching for them, and then experiment.)
Try this —
[X,Y] = meshgrid(-1000:10:1000);
Z = exp(-(X.^2+Y.^2)/1E5)*1E6;
figure
surf(X,Y,Z, 'EdgeColor','none')
grid on
colormap(turbo)
hcb = colorbar;
% get(hcb);
hcb.Ruler.Exponent = 3;
hcb.Ruler.Scale = 'log';
See NumericRuler Properties for more details.
.

8 comentarios

Mathan
Mathan el 7 de Abr. de 2022
Wow - thanks a bunch!
Star Strider
Star Strider el 7 de Abr. de 2022
As always, my pleasure!
I also learned a bit more about MATLAB documentation in discovering that!
Mathan
Mathan el 7 de Abr. de 2022
Definitely - thank you again!
Star Strider
Star Strider el 7 de Abr. de 2022
As always, my pleasure!
Mathan
Mathan el 7 de Abr. de 2022
Sorry but what does the 3 in hcb.Ruler.Exponent = 3 signify? I tried with different values other than 3 and am still receiving same outputs.
Thanks
Star Strider
Star Strider el 7 de Abr. de 2022
As always, my pleasure!
That is the level of exponents displayed. It shows differently on linear scaling, however on the logarithmic scaling, it just has to be set to some value. See Exponent for more information on it.
Mathan
Mathan el 7 de Abr. de 2022
Definitely - thanks!
Star Strider
Star Strider el 7 de Abr. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 7 de Abr. de 2022
Editada: Voss el 7 de Abr. de 2022
You can use naturlal log (log) or base-2 log (log2) (but I think log10 makes the most sense):
col = colorbar('XTickLabel', {'10^{5}', '10^{6}', '10^{7}'}, 'XTick',log(10.^(5:7))/log(10));
caxis([5 7])
figure();
col = colorbar('XTickLabel', {'10^{5}', '10^{6}', '10^{7}'}, 'XTick',log2(10.^(5:7))/log2(10));
caxis([5 7])

2 comentarios

Mathan
Mathan el 7 de Abr. de 2022
Thank you - basically the same thing as log10 I guess.
Voss
Voss el 7 de Abr. de 2022
Exactly. log10 with an extra log.

Iniciar sesión para comentar.

Preguntada:

el 7 de Abr. de 2022

Comentada:

el 7 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by