Shahgee
Shahgee

Reputation: 3405

How do I Label colorbar ticks in MATLAB?

I want to manually set my colorbar's tick labels and its position horizontal. For example:

 Min=0.8;       
 Max=12;    
 h = colorbar('horiz');       
 set(h,'location','southoutside')
 set(h,'XTickLabel',{num2str(Min),'mm'  ,num2str(Max)})

However, the above code repeats the tick labels label. How can I set number of tick manually? I want my colorbar to appear something like the following:

****----------------****         //colorbar
min        [mm]           max

Upvotes: 4

Views: 25634

Answers (1)

Bill Cheatham
Bill Cheatham

Reputation: 11947

You can set the locations of your ticks like so:

set(h, 'XTick', [Min, (Min+Max)/2, Max])

Be aware that this will probably not look correct, unless the colour limits of your plot are set to the range [0.8, 12]. You can do this with:

set(gca, 'CLim', [Min, Max])

Furthermore, a better way of adding the units 'mm' to your colourbar would be as follows:

h = colorbar('horiz');  
set(gca, 'CLim', [Min, Max])
set(h, 'XTick', [Min, Max])
set(h,'XTickLabel',{num2str(Min) ,num2str(Max)}) %# don't add units here...
xlabel(h, 'mm')                                  %# ...use xlabel to add units

Upvotes: 7

Related Questions