Hi, I am trying to give each bar a label. However, when I try to do it, it displays the label vertically, but I would like to have it horizontally otherwise it doesn't fit.. Does anyone know how to do that?? Thanks!!!
x = 1:1:15;
y = vMean(1:15);
name = {'gross', 'comp', 'rate', 'inv', 'gross', 'comp', 'rate', 'inv', 'gross', 'comp', 'rate', 'inv','gross', 'comp', 'rate'}
figure(6);
bar(x,y);
set(get(gca, 'XTicklabel'),'rotation', name);

1 commentaire

Jan
Jan le 27 Mai 2018
Modifié(e) : Jan le 27 Mai 2018
Today I've edited your message to improve the readability. I've selected the code and pressed the "{} Code" button.

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 27 Mai 2018
How about this:
x = 1:15;
y = randi([10 30],size(x));
name = {'gross', 'comp', 'rate', 'inv', 'gross', 'comp', 'rate', 'inv', 'gross', 'comp', 'rate', 'inv','gross', 'comp', 'rate'};
figure(6);clf(6)
bar(x,y);
set(gca, 'XTick',min(x):max(x))
set(gca, 'XTickLabel',name)
set(gca,'XTickLabelRotation', 90)

1 commentaire

Jan
Jan le 29 Mai 2018
+1. It can be important to set the XTicks also. The output is confusing, if the number of Ticks differ from the number of TickLabels. This does not happen for bar plots, but it for a general solution it is safer, to define them both.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 27 Mai 2018
Modifié(e) : Jan le 27 Mai 2018
set(get(gca, 'XTicklabel'),'rotation', name)
Almost. But:
get(gca, 'XTicklabel')
gets the strings of the XTickLabels as cell string. Not useful here. Better:
set(gca, 'XTicklabel', name, 'XTickLabelRotation', 90)

1 commentaire

Or, for versions later than R2014b
ax = gca;
ax.XTickLabel = name;
ax.XTickLabelRotation = 90;
if you want the OOP way of setting properties like other languages use.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by