how to print the values of each bar on top of it; multibar and subplots
1 次查看(过去 30 天)
显示 更早的评论
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.
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 个评论
采纳的回答
Image Analyst
2013-1-4
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 个评论
更多回答(2 个)
Teja Muppirala
2013-1-4
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])
Hello kity
2013-1-3
编辑:Hello kity
2013-1-3
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!