Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to print the values of each bar on top of it; multibar and subplots

Asked by David on 3 Jan 2013

Hello, I'm desperately searching an easy way to print my values on my bars. At the moment I have this: https://imageshack.us/photo/my-images/59/barsm.jpg/ Sorry for the bad quality. As you perhaps can see I have characters only under every bar or additionally in or over every bar. I only want the values once per diagramm and I think the most eye appealing solution would be right on top of every bar.

I used the code from here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/283510

Unfortunately I don't understand this code, so I don't know whats going wrong here and cant correct it.

This is the code:

    % code
% the data
     d=[
          4 8 9 7 4
          8 7 5 5 8
          2 7 1 4 9
     ];
     lab={
          'ab' 'bc' 'ef' 'ww' 'qq'
          'q' 'd' 'c' 'w' 'r'
          'w' 'i' 'yr' 'w' 'r'
     };
% the engine
     bh=bar(d);
     xd=get(bh,'children');
     xd=get([xd{:}],'xdata');
     xd=cat(2,xd{:});
     xdd=diff(xd);
     xd=sort(xd(1,:)+.5*xdd(2,1));
     set(gca,'xtick',xd);
     set(gca,'xticklabel',lab.');
     yl=get(gca,'ylim');
     set(gca,'ylim',[yl(1),yl(2)+3]);
     text(xd,repmat(11,1,numel(xd)),lab.','horizontalalignment','center');

0 Comments

David

Products

No products are associated with this question.

3 Answers

Answer by Image Analyst on 4 Jan 2013
Accepted answer

Replace the last line:

text(xd,repmat(11,1,numel(xd)),lab.','horizontalalignment','center');

with these two lines:

yValues = d' + 0.5;
text(xd, yValues(:),lab.','horizontalalignment','center');

0 Comments

Image Analyst
Answer by Teja Muppirala on 4 Jan 2013

Would something like this work?

Data1 = [1 2 0 3 4];
Data2 = [2 3 4 5 5];
hb = bar([Data1;Data2]);
% Find the x location of each bar  
xvals = unique(cell2mat(get(findall(hb,'type','patch'),'xdata')));
xvals = mean(reshape(xvals,2,[]));
% Put the text there
text(xvals,[Data1 Data2],mat2cell([Data1 Data2]),...
    'Vert','bot','horiz','cen','FontName','Arial','Fontsize',12);
ylim([0 6])

1 Comment

David on 4 Jan 2013

nicely done. Also a very good answer. Can you tell me how I can use a matrix as input instead of arrays?

Teja Muppirala
Answer by Hello kity on 3 Jan 2013
Edited by Hello kity on 3 Jan 2013

I was doing the same, found this for you:

   x = [1 2 0 3 4];
  xname = {'Foo','Bar','','Baz','Othername'};
  bar(x)
  text(1:numel(x),x,xname,'horizontalalignment','center','verticalalignment','bottom') 

in case you use more variable in one bar then do following

for example

xname = {'Data1', 'Data2'};
bar(1, Data1, 'r')
hold on
bar(2, Data2, 'g')
  text(1:2,[Data1 Data2], etc)

1 Comment

David on 4 Jan 2013

Thank You!!! Can I use the second solution when I have a second variable?

I used:

Data1 = [1 2 0 3 4]

Data2 = [2 3 4 5 5]

But it tells me:

Error using bar (line 55)

X must be same length as Y.

But I have for one X multiple Y and I have multiple Xs.

If I use your solution I will have:

 y y y y y y y y y y 
 x1x1x1x1x1x2x2x2x2x2

I need:

 y y y y y   y y y y y 
    x1           x2

So perhaps can I do this if I use your solution and only let with Text the x1 and x2 label appear once:

   y y y y y y y y y y 
      x1          x2

But how can I have the white space between the bars:

 y y y y y   y y y y y 
    x1           x2

I believe the step from Excel to Matlab was not my best decision.

Hello kity

Contact us