Bar plot with different label for each bar
7 views (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159367/image.jpeg)
but I would like to have it in this way:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159370/image.png)
or to have labels inside the bars.
Thank you in advance!
Cheers, Marko
0 Comments
Answers (3)
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...
0 Comments
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.
0 Comments
See Also
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!