Matlab 中带标签的条形图

Bar plot with labels in Matlab

如何在 Matlab 中简单地将当前值的垂直标签添加到 bar plot

我想添加当前值,现在 "here" 是:

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(虽然从 HG2 开始,内部结构发生了很大变化,因此进入内部并检索我们需要的数据变得更加棘手)。

如果您愿意深入挖掘,这里有一个适用于 MATLAB R2014b 和更新版本的解决方案(请注意,我正在使用未记录的属性来获取由 bar 创建的隐藏 "Face" 图形对象情节):

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