how to label multiple bars within the same x-value

16 views (last 30 days)
Hej im trying to create a plot of some testvalues, and i simply cant figure out a way to label multiple bars within the same x-value.
The test data comes out in a 6 x M matrix telling the time datacollection time and a 4 x M matrix with the testdata were each column is zone1, zone2, zone3 and zone4.
i've tried this so far:
Zones={'zone1','zone2','zone3','zone4'};
X=tvec_a(:,4);
Y=data_a;
bar(X,Y,1)
set(gca,'xticklabel',Zones)
title('Hourly consumption')
xlabel('Hour')
ylabel('Consumption')
But this simply output all four columns with the same same instead of each column was devided into zones for each x-val:

Accepted Answer

Joakim Mørk
Joakim Mørk on 6 May 2020
After a whole lot of research, I've found nothing that solves that excact problem. Allthough I ended up finding two solutions wich solves the issue of telling the individual bars from oneanother:
1) using legend:
b=bar(tvec_a(:,N),data_a,1);
title(name)
xlabel(X)
ylabel('Consumption')
axis([-inf inf -inf inf])
legend({'zone 1','zone 2', 'zone 3', 'zone 4'},'Location','northwest')
grid on
2) using bar labels
b=bar(tvec_a(:,N),data_a,1);
title(name)
xlabel(X)
ylabel('Consumption')
xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;
labels1 = 'z_1';
text(xtips1,ytips1,labels1,'HorizontalAlignment','center',...
'VerticalAlignment','top','color','w')
xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;
labels2 = 'z_2';
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','top','color','w')
xtips3 = b(3).XEndPoints;
ytips3 = b(3).YEndPoints;
labels3 = 'z_2';
text(xtips3,ytips3,labels3,'HorizontalAlignment','center',...
'VerticalAlignment','top','color','w')
xtips4 = b(4).XEndPoints;
ytips4 = b(4).YEndPoints;
labels4 = 'z_2';
text(xtips4,ytips4,labels4,'HorizontalAlignment','center',...
'VerticalAlignment','top','color','w')
axis([-inf inf -inf inf])
grid on
I personally thinks the legend is the better sollution

More Answers (0)

Categories

Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!