Setting Ticks in a Colorbar

214 visualizaciones (últimos 30 días)
Natalya
Natalya el 12 de En. de 2013
Comentada: Nike el 23 de Feb. de 2018
How do I set the ticks in a color bar to be at specific values?
For example, when I just use the command colorbar('h'), Matlab automatically generates a colorbar with ticks at -10, -5, 0,5,10. I would like the ticks to be at -12 -9,-6,-3,0,3,6,9,12. I tried:
cbh=colorbar('h');
set(cbh,'XTick',[-12:3:12])
This didn't change anything.
Adding the line:
set(cbh,'XTickLabel',{'-12','-9','-6','-3','0','3','6','9','12'})
just relabeled the ticks, but did not change their location (i.e. the value of -10 got labeled '-12')
I also tried this:
cbh=colorbar('h');
cy=get(cbh,'XTick');
set(cy,[-12:3:12])
This set ticks at smaller intervals and labeled them from -70 to 10... I do not understand why.
Does anyone have any other suggestions?
Thanks!
  2 comentarios
Nicholas DeWind
Nicholas DeWind el 9 de Nov. de 2015
Use YTick:
cbh=colorbar('h');
set(cbh,'YTick',[-12:3:12])
Nike
Nike el 23 de Feb. de 2018
That's an easy and effective solution. Thanks

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 12 de En. de 2013
Matlab's colorbar command creates an image object. See:
cbh = colorbar('h');
get(get(cbh, 'Children'))
>> ...
CData = [ (1 by 64) double array]
DataMapping = direct
XData = [1.5 64.5]
YData = [0 1]
...
Type = image
When you want to change the ticks from -12:3:12, I guess you want 25 different colors in the colorbar.
AxesH = axes('CLim', [-12, 12]);
cbh = colorbar('peer', AxesH, 'h', ...
'XTickLabel',{'-12','-9','-6','-3','0','3','6','9','12'}, ...
'XTick', -12:3:12)

Categorías

Más información sobre Colorbar en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by