How can I simply add vertical labels of the current value to bar plot in Matlab?
I want to add the current value where now "here" is:
How can I simply add vertical labels of the current value to bar plot in Matlab?
I want to add the current value where now "here" is:
The question I linked to in the comments is one way to do it. There are other ways to customize bar plots, for instance see this article (although starting with HG2, the internals have changed considerably, so it got a trickier to reach inside and retrieve the data we need).
If you're willing to dig deep, here is a solution that should work with MATLAB R2014b and newer (note that I'm using undocumented properties to get the hidden "Face" graphic objects created by the bar plot):
Y = rand(3,4);
h = bar(Y);
drawnow % this is needed for some reason!
opts = {'VerticalAlign','middle', 'HorizontalAlign','left', ...
'FontSize',8, 'Rotation',90};
for i=1:numel(h)
clr = h(i).Face.ColorData(1:3);
vd = h(i).Face.VertexData;
xy = double(vd(1:2,2:4:end) + vd(1:2,4:4:end)) / 2;
for j=1:size(xy,2)
text(xy(1,j), xy(2,j), sprintf(' %.2g',xy(2,j)), ...
'Color','k', opts{:})
end
end
