Plotting confidence interval with bar plot

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?

4 Comments

Hey Amy!!
it's very unclear by 'these lines fit exactly to my x-axis bars' because the plot of mean doesn't need to be a line and would plot the means on each point.
Could you elaborate a little more on your question, and also give an example (if possible) for better understanding?
Hi Gaurav,
Thank you for your message! I shall try to explain more and be a bit clearer.
In this graph, the black line, and the grey shaded area represet a group that I would like to compare my other groups (represented by the bars and individual points) to. So, I would like the shaded grey region to be horizontal in the space behind the bars, because the three bars have the same "x" value.
Is that any clearer?
How are you plotting the black line and grey shaded area?
Using this code:
%%Plot figure
h = figure;
x = 1:numel(Behaviours); %% x axis values
yValues = norm_WT_std2_SH_allInfo(1, :); %% y values
plot(x, yValues, 'k-'); %plot black line
hold on;
plot(x, norm_WT_std2_SH_allInfo(2, :), 'LineWidth', 0.5, 'Color', 'white'); % upper boarder of shaded area
hold on;
plot(x, norm_WT_std2_SH_allInfo(3, :), 'LineWidth', 0.5 , 'Color', 'white'); % lower boarder of shaded area
x2 = [x, fliplr(x)]; %% code to fill in this area (from another matlab answers question
inBetween = [norm_WT_std2_SH_allInfo(2, :), fliplr(norm_WT_std2_SH_allInfo(3, :))];
f= fill(x2, inBetween, 'k');
set(f,'facealpha',.1)
set(f,'edgecolor','white');
set(gca,'TickDir','out');
hold on;

Sign in to comment.

 Accepted 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;

More Answers (1)

Hey Amy,
The grey shaded region seem to have varying y-values for the respective x-values.
What is evident from your code is that norm_WT_std2_SH_allInfo is an array of dimensions m*3, and borders of the shaded region are plotted using the second and third columns of the array. So, I would suggest you to look into the values in this array and change them (if needed) to make your grey shaded region to be a straight horizontal region.
The needed changes can be understood as -
You can plot any line parallel to x-axis if the y-value at each x-point for the line is 0.

Categories

Community Treasure Hunt

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

Start Hunting!