How do I insert values into my bar plot (with 2 columns per x value)?

1 view (last 30 days)
If I have a vector A and matrix B and make a bar plot, how do I list the corresponding y values at the top of each bar? (I understand that you use the text() function but I cannot get it to work). An example of what I want to accomplish:
A = [1 2 3 4 5]; B = [4 7; 2 9; 3 7; 4 8; 1 3]; bar(A,B);
% how do I properly use text() or something else to insert the data values located in B??

Answers (1)

Conrad
Conrad on 30 Jan 2013
You can try something like this:
A = [1 2 3 4 5];
B = [4 7; 2 9; 3 7; 4 8; 1 3];
bar(A,B);
xOffsets = [-0.16 0.16];
yOffsets = [0.16 0.16];
for i = 1 : size(B,2)
text(A + xOffsets(i), B(:,i)' + yOffsets(i), num2str(B(:,i)));
end
Conrad

Products

Community Treasure Hunt

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

Start Hunting!