2

Problem

I am generating a bar chart and would like to display the height of each bar above the bar itself (Ydata). So for the following picture which is an example, I would like to have labels above the charts. I couldnt find a solution to this. For your information I am using Matlab R2016a.

enter image description here

Code

Currently I am using the following code to create my chart.

   x={ '-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35' '35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0]
after= [27 28 18 9 6 5 3 2 1 1 0 0]
y=[before',after']

h=figure;
hold on
yyaxis left
l1=bar([1:12],y,'grouped');

hYLabel=ylabel('Tonnage [%]');
yyaxis right
hylabel=ylabel('Tonnage [%]');
l1(1).FaceColor = [ 0    0.447  0.7410];
l1(1).EdgeColor = [ 0    0.447  0.7410];
l1(2).FaceColor =[0.85 0.325 0.098]
l1(2).EdgeColor =[0.85 0.325 0.098]
hTitle=title('Test');
hXLabel = xlabel('Value [$/t]');
hLegend=legend([l1(1),l1(2)], 'Test1', 'Test2');
set([gca,hTitle,hXLabel,hYLabel,hLegend] , 'FontName'   , 'Helvetica','FontSize', 8) 
set(hTitle,'FontSize', 11) 
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'XTick',[1:12])
xlim([0.5 12.5])
set(gca,'xticklabel',x.')
set(gca,'LineWidth',1.0)

hold off

What I am looking for Quick illustration of what I am looking for. Obviously I want a label above each column. Any help would be very appreciated.

enter image description here

EBH
  • 10,350
  • 3
  • 34
  • 59
KiW
  • 593
  • 3
  • 20

2 Answers2

5

After your line:

l1=bar([1:12],y,'grouped');

add following lines:

x_shift = 0.15;
text([1:12]-x_shift,y(:,1)+1,num2str(y(:,1)),...
    'FontSize',12,'HorizontalAlignment','Center','Color',[0 0.447  0.7410])
text([1:12]+x_shift,y(:,2)+1,num2str(y(:,2)),...
    'FontSize',12,'HorizontalAlignment','Center','Color',[0.85 0.325 0.098])

And you will get:

Labeled bar

if you want the percentage format, and also rotation, then the x_shift need to be adjusted a little bit more, and also the y-axis limits, so I bring here the full code for that:

x={'-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35'...
    '35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0];
after= [27 28 18 9 6 5 3 2 1 1 0 0];
y=[before',after'];
ax = axes('xticklabel',x.','LineWidth',1.0,'XTick',1:12);
yyaxis(ax,'left')
l1 = bar(ax,y,'grouped');
x1_shift = -0.17;
x2_shift = 0.11;
text([1:12]+x1_shift,y(:,1)+1,[num2str(y(:,1)) repmat('%',numel(y(:,1)),1)],...
    'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
    'VerticalAlignment','middle','Color',[0 0.447  0.7410])
text([1:12]+x2_shift,y(:,2)+1,[num2str(y(:,2)) repmat('%',numel(y(:,2)),1)],...
    'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
    'VerticalAlignment','middle','Color',[0.85 0.325 0.098])
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
ylim([0 35])
yyaxis(ax,'right')
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
l1(1).FaceColor = [0 0.447  0.7410];
l1(1).EdgeColor = [0 0.447  0.7410];
l1(2).FaceColor = [0.85 0.325 0.098];
l1(2).EdgeColor = [0.85 0.325 0.098];
title('Test','FontName','Helvetica','FontSize', 11);
xlabel('Value [$/t]', 'FontName'   , 'Helvetica','FontSize', 8);
hLegend = legend([l1(1),l1(2)], 'Test1', 'Test2');
set(hLegend,'Location','southoutside','Orientation','horizontal',...
    'FontName', 'Helvetica','FontSize', 8)
xlim([0.5 12.5])
ylim([0 35])
box off

you will notice I changed your code a little, to make it more compact, but essentially it does the same, and yields the following bar:

 percentage format

the labels here will be placed in the same position (relative to the bars) even if you resize the graph.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • Thanks for that answer, but is there no way of getting them completly centered even if you change the size of the figure ? – KiW Sep 08 '16 at 10:15
  • @KiW I have edited the code above so that the labels will stay centered. I also added the rotation and percentage format if you want these. – EBH Sep 08 '16 at 14:53
1

You can use something like in this post Add before hold off this line:
text(1 , y(1,1)+30, ['y = ', num2str(10)], 'VerticalAlignment', 'top', 'FontSize', 8)
Now you can play with the parameters and put it into a for loop to add labels over evrey bar.

Edit:
If I understood you right, this is what you wanted?
Add the following before hold off

xt1=[1:12]-0.17;
xt2=[1:12]+0.11;
yt1=before+0.2;
yt2=after+0.2;

for i=1:12
    text(xt1(i) , yt1(i), [num2str(y(i,1)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0 0.447  0.7410])
    text(xt2(i) , yt2(i), [num2str(y(i,2)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0.85 0.325 0.098])
end

Now you can also change the size of the figure and the texts stay at the desired position.
The result looks like the following: Bar Graph with Labels for every bar By the way, I had only access to Matlab2015, so I couldn't use every feature of yours, but the code should be fine on Matlab2016

Community
  • 1
  • 1
chron0x
  • 875
  • 9
  • 19