How to add labels to cluster bar graphs

조회 수: 3 (최근 30일)
Stefano
Stefano 2014년 10월 22일
답변: Star Strider 2014년 10월 23일
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

답변 (1개)

Star Strider
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.)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by