Xtick label alignment in bar plot

31 visualizaciones (últimos 30 días)
Umz
Umz el 23 de Jun. de 2016
Respondida: dpb el 23 de Jun. de 2016
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]);

Respuesta aceptada

dpb
dpb el 23 de Jun. de 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'}.';

Más respuestas (0)

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by