Add another label that groups the bars in a bar plot together

1 view (last 30 days)
Hi,
I have created a bar plot that plots four groups of bars with four bars in each group. All bars with the same color, one bar in each group, represent a measurement from a test with the same material. Each group of bars is one test where two groups are measurements from an even surface and two groups are measurements from uneven surfaces.
Now, I'd like to add another row below the below the ‘Even’ and ‘Uneven’ labels, pairing the two left groups together and the two right two groups together since the tests are performed at two different locations. On this second row I want to add labels showing the test location. I have a screenshot from excel showing what I want to do. The test locations in this plot are named 'KER-50' and 'BUT-70'
Is there anyone out there who has experience about this?
clear all, close all, clc
Homepath = cd;
A = [80,74,84,78
49,44,53,48
-8.9,-9.2,-6.9,-7.2
-9.1,-10,-8.1,-8.3];
figure1 = figure;
width = 4;
height = width*0.8;
axes1 = axes('Parent',figure1);
hold(axes1,'on');
bar(A,'Parent',axes1);
ylim(axes1,[-20,100])
set(axes1,'XTick',[1 2 3 4],...
'XTickLabel',{'Even','Uneven','Even','Uneven'},...
'YTick',[-20,0,20,40,60,80,100],...
'YTickLabel',{'-20','0','20','40','60','80','100'});
legend(axes1,'HMA 3%','HMA 0%','RHMA 3%','RHMA 0%')
box(axes1,'on')
ylabel(axes1,'Energy savings (10^6 MJ)')
cd([Homepath '\PICS'])
print EnergySavings_5years -djpeg -r600
cd(Homepath)
EnergySavings_5years.jpg
total_energy_saving_5years.png

Accepted Answer

Adam Danz
Adam Danz on 9 Oct 2019
Edited: Adam Danz on 9 Oct 2019
Add this to the end of your code.
By the way, avoid using cd() by specifying full paths whenever possible.
ylim([-40,100])
yl = ylim(axes1);
xl = xlim(axes1);
plot(xl,[-20,-20], 'k-') %horz line at y=-20
plot(range(xl)./[2,2] + min(xl),[yl(1),0], 'k-') %vert line connecting y=0 and y=20
textLoc = range(xl).*[.25,.75] + xl(1); %1/4 and 3/4 of x axis limit
text(textLoc, [-30,-30], {'KER-50','BUT-70'},'HorizontalAlignment','center')
  4 Comments
Henrik Bjurström
Henrik Bjurström on 9 Oct 2019
Ok, I will look in to that and try to learn something new :)
Thanks for answering!
Adam Danz
Adam Danz on 9 Oct 2019
Glad I could help! Of course you should do your own cost-benefit analysis before revamping your working code to transition from cd() to full paths. Maybe it's just something to keep in mind for future projects. Using full paths offers much better control.
If others are using your code, cd() is definitely a bad choice. Another potential problem cd() may cause is adding a path from a remote network to your matlab cd history. Once the machine no longer has access to the remote network, simple errors such as an undefined function may consume up to a minute before Matlab throws the error message.

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!