필터 지우기
필터 지우기

Xtick label alignment in bar plot

조회 수: 33 (최근 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 CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by