Xtick label alignment in bar plot
34 views (last 30 days)
Show older comments
I am having problem with the xticks in the bar plot. I would like to align the label names along the horizontal line. I am unable to figure out the solution. Any help is greatly appreciated.
A = [30, 10, 7, 4, 4, 3, 2].';
figure(1);
set(gcf,'color','w');
x = (1:size(A,1)).';
myplot = bar(A, 'stacked');
Variables = char('First', 'Second', 'Third',...
'Fourht', 'Large Name', 'Very Large Name', 'Small Name');
t= text(x+.3,0*ones(1,size(x,1)),Variables(1:size(A,1),:));
set(gca,'XTickLabel','');
set(t,'HorizontalAlignment','right','VerticalAlignment','top', ...
'Rotation',45, 'Fontsize', 13);
ylabel('Contributor %', 'Fontsize', 13);
set(gca, 'OuterPosition', [0.02 0.2 1 0.8]);
0 Comments
Accepted Answer
dpb
on 23 Jun 2016
Variables = char('First', 'Second', 'Third',...
'Fourth', 'Large Name', 'Very Large Name', 'Small Name');
causes the strings to be appended with blanks to the same length; hence your strings are all the same length and the 'horizontalalignment','right' looks funky compared to what you're expecting.
The fix is use cellstrings instead...
Variables = {'First','Second','Third','Fourth','Large Name','Very Large Name','Small Name'}.';
0 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots 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!