How to add labels to cluster bar graphs

3 visualizaciones (últimos 30 días)
Stefano
Stefano el 22 de Oct. de 2014
Respondida: Star Strider el 23 de Oct. de 2014
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

Respuestas (1)

Star Strider
Star Strider el 23 de Oct. de 2014
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.)

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by