Hi,
I have been trying to add title to my colorbar using the following lines of code:
col=colorbar;
colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
caxis([log10(1e10) log10(1e12)])
ylabel(col,'My Title')
It however gives me a colorbar without the title. I then changed the position of the codes as:
colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
caxis([log10(1e10) log10(1e12)])
col=colorbar;
ylabel(col,'My Title')
and this time it gives me the colorbar with the title but the xticks are not what I had mentioned.
Wondering what I am doing wrong here and why changing the sequence of codes have such a dramtic effect.
Thanks.

 Respuesta aceptada

Voss
Voss el 6 de Abr. de 2022
col = colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
caxis([log10(1e10) log10(1e12)])
ylabel(col,'My Title')

4 comentarios

Mathan
Mathan el 6 de Abr. de 2022
Perfect, it works. Thank you so much!
Do you happen to know what my lines of codes were doing (especially getting different results when I simply reshuffled the lines of codes).
You're welcome!
Basically, each time you call colorbar a new colorbar is created, which deletes any previous colorbars you had, in this case.
Here's the three attempts (including mine), with comments explaining what happens with the colorbars:
% Try 1
figure()
% make a colorbar
col=colorbar;
% make another colorbar with these XTicks, etc.
% (deletes the 'col' colorbar)
colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
% col no longer exists:
ishandle(col)
ans = logical
0
% set the color limits of the axes
caxis([log10(1e10) log10(1e12)])
try
% ylabel the col colorbar that doesn't exist anymore -> error
ylabel(col,'My Title')
catch ME
disp(sprintf('Error in ylabel: %s',ME.message));
end
Error in ylabel: Incorrect number of input arguments
% Try 2
figure()
% make a colorbar with these XTicks, etc.
colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
% set the color limits of the axes
caxis([log10(1e10) log10(1e12)])
% make a new colorbar (has default XTicks etc.) -> old colorbar is deleted
col=colorbar;
% label the new colorbar -> ok but you don't have the old one anymore
ylabel(col,'My Title')
% Solution
figure()
% make a colorbar with these XTicks, etc., store it as 'col'
col = colorbar('XTickLabel', {'10^{10}', '10^{11}', '10^{12}'}, 'XTick',log10(1e10):1:log10(1e12));
% set the color limits of the axes
caxis([log10(1e10) log10(1e12)])
% ylabel colorbar col
ylabel(col,'My Title')
Mathan
Mathan el 6 de Abr. de 2022
Wow - thank you for the detailed answer. Makes perfect sense.
Thanks once again!
Voss
Voss el 6 de Abr. de 2022
Happy to help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 6 de Abr. de 2022

Comentada:

el 6 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