Xtick label alignment in bar plot

30 ビュー (過去 30 日間)
Umz
Umz 2016 年 6 月 23 日
回答済み: dpb 2016 年 6 月 23 日
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]);

採用された回答

dpb
dpb 2016 年 6 月 23 日
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 件)

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by