Bar plot with different label for each bar

5 views (last 30 days)
Marko Baros
Marko Baros on 24 Mar 2014
Answered: Star Strider on 24 Mar 2014
Hello,
I would like to ask someone for help about labeling specific bar with specific name. I've done it, but since the labels couldn't be shorter, I want to know if these labels are able to be rotated or written vertically?
Part of script:
figure
bar([1:22],Qm)
xlabel('Different sequences')
ylabel('Mean qualities')
title('Distribution of mean qualities')
set(gca, 'XTick', 1:22, 'XTickLabel', labels);
%labels is a vector of strings
and this look like this:
but I would like to have it in this way:
or to have labels inside the bars.
Thank you in advance!
Cheers, Marko

Answers (3)

dpb
dpb on 24 Mar 2014
Tick labels don't have the facility to be modified other than font characteristics but you can use text to label wherever you wish and use TeX and other features of the text object to orient to your heart's content.
Start with
doc text
and follow the links to the properties available and examples. There's a section w/ such in the Graphics chapter under Annotation that's a good intro...

Marko Baros
Marko Baros on 24 Mar 2014
Hey, I got idea and I just had to switch axes using barh, so I got this:
but the thing is that I would need to sort the bars in ascending or descending way, but not sure how to do it.

Star Strider
Star Strider on 24 Mar 2014
I remember doing this before, but not very recently. The text idea is definitely the solution:
fr = rand(100,1);
[c b] = hist(fr);
for k1 = 1:length(b)
barlbl{k1} = sprintf('Bar # %2d\n', k1);
end
figure(1)
bar(b, c)
set(gca,'XTick',1:length(b))
set(gca,'XTickLabel', text(b,ones(size(b))-1.5, barlbl,'Rotation',45,'FontSize',7, 'HorizontalAlignment','right'))
axis tight
There is no truly automatic way to position labels of varying length, so you’ll have to experiment to get the result you want.

Community Treasure Hunt

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

Start Hunting!