How to add labels to cluster bar graphs
3 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to code in matlab how to exactly:
1. create clusters of bar graphs. So I want to create two bar graphs beside one another comparing two values in a bar chart format, and I need to do it for 4 seperate sets of data in the same graph.
2. Also on top of these bar graphs I would like to have the data label placed on top (so the value of the bar graph can be seen)
Any Help would be much appriciated! cheers
0 件のコメント
回答 (1 件)
Star Strider
2014 年 10 月 23 日
This works in R2014b:
x = randi(10, 4, 2);
figure(1)
hb = bar(x)
axis([xlim 0 15])
hdx = get(hb(1),'XData');
hdy = get(hb(1),'YData');
hbw = get(hb(1),'BarWidth')
Lbs1 = strsplit(sprintf('%d ',hdy));
text(hdx-hbw/5,hdy, Lbs1(1:end-1),'HorizontalAlignment','center','VerticalAlignment','bottom')
hdx = get(hb(2),'XData');
hdy = get(hb(2),'YData');
hbw = get(hb(2),'BarWidth')
Lbs2 = strsplit(sprintf('%d ',hdy));
text(hdx+hbw/5,hdy, Lbs2(1:end-1),'HorizontalAlignment','center','VerticalAlignment','bottom')
producing:
It requires the ‘hbw/5’ fudge to get the labels to line up. In previous versions of handle graphics, it was possible to get the necessary data to calculate the exact centres of the bars. That code no longer works in R2014b. (I need to contact Tech Support to find out how to do that in HG2, if it’s still possible.)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Bar Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!