MATLAB: How to creat bar plots like the picture based on the existing data in attached Excel.

bar plotsMATLABtwo y axes

Best Answer

It's not the easiest figure to reproduce, as it involves double yaxis, errorbars and hatched fill. I have written a script for the two former ones. For the hatched fill, I recommend hatchfill from fileexchange ( link ). For some reason, I could not access the XData from the individuals bars, so the XData for the errorbars were here specified manually as the XData for the group +- a variable called space.
data=readtable('Attachment 2.xls')
figure;hold on
xlabel('Extrusion rate, mm/s');
ylabel('Stress, MPa');
nans=nan(length(data.Elongation__),1);
hBar1=bar([data.ExtrusionRate_mm],[data.YS_Stress,data.UTS_Stress,nans]);
set(gca,'ylim',[0 450])
%adding error bars
space=0.22;
errorbar(data.ExtrusionRate_mm-space, data.YS_Stress, data.ErrorOfYS/2,'.','color',[0 0 0]);
errorbar(data.ExtrusionRate_mm+0, data.UTS_Stress, data.ErrorOfYS/2,'.','color',[0 0 0]);
%create second yaxis
yyaxis right
ylabel('Elongation, %')
hBar2=bar(data.ExtrusionRate_mm,[nans,nans,data.Elongation__],'group');
set(gca,'ylim',[0 16])
%add error bar
u=errorbar(data.ExtrusionRate_mm+space, data.Elongation__, data.ErrorOfElongation/2,'.','color',[0 0 0]);
%change colors and fix other issues
hBar1(1).FaceColor=[0 0 1];
hBar1(2).FaceColor=[1 0 0];
hBar2(3).FaceColor=[0 1 0];
set(gca,'xtick',[1 2 3 4]);
set(gcf,'color',[1 1 1]);
box on
leg=legend([hBar1(1),hBar1(2),hBar2(3)],'YS','UTS','Elong');
set(leg,'box','off','location','northwest');
Hope it helps!