gabboshow
gabboshow

Reputation: 5559

add colorbar with yticklabes matlab

I run Matlab R2013b

I would like to run a colorbar to my graph but instead of 4 labels as specified

figure
plot(1:100,rand(100))
hcb = colorbar('YTickLabel',{'Sleeping','Very light','Light','Moderate to vigorous'});

I get 6 labels: {'Sleeping','Very light','Light','Moderate to vigorous','Sleeping','Very light'}

Upvotes: 0

Views: 242

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

colorbar by default uses a colormap of 64 colors. That causes Matlab, by default, to place 6 yticks in the colorbar, namely at 10, 20, ... 60.

When you set the 'Yticklabel' property, if you pass less strings than the number of yticks, those strings are cycled over. That's the behavior you observe.

The solution is to reduce the number of yticks to 4, so that it matches the number of strings you have. You may also want to use a 4-color colormap:

figure
plot(1:100,rand(100))
colormap(hsv(4)) %// example colormap with 4 colors.
hcb = colorbar;
set(hcb, 'Ytick', [1:4]+.5); %// 4 yticks, each "in the middle" of one color
set(hcb, 'YTickLabel', {'Sleeping','Very light','Light','Moderate to vigorous'});

enter image description here

Upvotes: 4

am304
am304

Reputation: 13876

I think you need to also specify a vector Ticks of the same length, e.g. (syntax for R2014b, may be slightly different for R2013b):

colorbar('Ticks',[0 0.3 0.6 0.9],...
         'TickLabels',{'Sleeping','Very light','Light','Moderate to vigorous'})

Upvotes: 1

Related Questions