MATLAB: Plotting confidence interval with bar plot

barfillmultiple xplotting

Hi all,
I have the following graph, and I want to plot the mean and standard deviation (in grey) such that the these lines fit exactly to my x-axis bars. I would really like the grey shaded region to be a straight horizontal line in the region behind hte bars themselves, and only "slope" in the spaces between. Is there a way that I can do this?

Best Answer

Hi All,
I actually found out how do this, to produce the following graph
and here is the code, should it be of any use to anyone
%%code for getting variables etc....
%%Bars
YValues = vertcat(barSet1, barSet2, barSet3 );
b = bar(x, YValues, 'FaceAlpha', 0.5, 'EdgeColor', 'none');
%%axes
drawnow;
hold on;
set(gca,'XTick',1:numel(Behaviours),'XTickLabel',Behaviours);
set(gca, 'FontName', 'Arial')
xtickangle(45);
hold on;
%%Scatter plots:
for i = 1:size(scatterPoints1,1)
s(1) = scatter(x+b(1).XOffset, scatterPoints1(i,:), 50, 'MarkerFaceColor', '#ff4343', 'MarkerEdgeColor', '#ff4343')
hold on
end
X_pos_firstBar =x+b(1).XOffset %% this gives the x-locations of each bar
for i = 1:size(scatterPoints2,1)
s(2) = scatter(x+b(2).XOffset, scatterPoints2(i,:),50, 'MarkerFaceColor', '#ed7023', 'MarkerEdgeColor', '#ed7023')
hold on
end
X_pos_secondBar =x+b(2).XOffset
for i = 1:size(scatterPoints3,1)
s(3) = scatter(x+b(3).XOffset, scatterPoints3(i,:),50, 'MarkerFaceColor', '#ed7023', 'MarkerEdgeColor', '#ed7023')
hold on
end
X_pos_thirdBar =x+b(3).XOffset
%%code for grey region
xValues = reshape ([ X_pos_firstBar ; X_pos_secondBar; X_pos_thirdBar], size(X_pos_firstBar,2), [] );
xValues = xValues(:)';
upper_line= repelem(greyRegionPoints(2,:), 2); %%repeats these points so that the region behind each of the three bars has the same value (gives straight line)
lower_line = repelem(greyRegionPoints(3,:), 2);
h = figure;
x = 1:numel(Behaviours);
yValues = norm_WT_obj_SH_allInfo(1, :); %straight line
plot(x, yValues, 'k-'); %plot straight line
% ylim([0, ((max(max(norm_WT_obj_SH_allInfo))*1.5))]);
ylim([0 inf])
hold on;
plot(xValues, upper_line, 'LineWidth', 0.5, 'Color', 'white'); %plot line that should be upper interval line
hold on;
plot(xValues, lower_line, 'LineWidth', 0.5 , 'Color', 'white'); %plot line that should be lower interval line
x2 = [xValues, fliplr(xValues)];
inBetween = [upper_line, fliplr(lower_line)];
f= fill(x2, inBetween, 'k');
set(f,'facealpha',.1)
set(f,'edgecolor','white');
set(gca,'TickDir','out');
hold on;