I've got a problem to find the exact location of a MATLAB bar-plot with multiple bars. Using the following code
A =[2.1974e-01 4.1398e-01 1.0889e-01 3.3550e-01;
4.2575e-01 5.2680e-01 2.3446e-01 9.7119e-02;
2.5702e+00 2.5594e+00 3.2481e+00 9.9964e-01];
b=bar(A);
I get the following plot
Now I want to add stuff to that plot, e.g. error bars, text etc. For that reason I want to know the exact position of the individual bars.
I'm able to access individual properties using b(1).
scheme, but I don't know which property belongs to the bar position. How do I get the exact location of each individual bar?
You are on the right track with the properties of
b = bar(A);
The specific properties you need are
b.XOffset
The spacing between groups of barsb.XData
The index of each group of barsb.YData
The height of each barFor the y-coordinates of the top of each bar, you can simply concatenate the `b.YData values.
yb = cat(1, b.YData);
For the x-coordinates, you need to add the offset to the indices
xb = bsxfun(@plus, b(1).XData, [b.XOffset]');
Now, you have the location of the top of each bar. Here's an error bar example.
figure;
bar(A)
hold on;
for ii = 1:length(xb(:))
plot([xb(ii), xb(ii)], [yb(ii)-0.1 yb(ii)+0.1], 'xk-')
end