MATLAB Answers

Plotting confidence interval with bar plot

59 views (last 30 days)
Amy Hassett
Amy Hassett on 6 Jun 2020
Answered: Amy Hassett on 16 Jun 2020
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

Show 1 older comment
Amy Hassett
Amy Hassett on 9 Jun 2020
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?
Gaurav Garg
Gaurav Garg on 9 Jun 2020
How are you plotting the black line and grey shaded area?
Amy Hassett
Amy Hassett on 10 Jun 2020
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

Amy Hassett
Amy Hassett on 16 Jun 2020
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;

  0 Comments

Sign in to comment.

More Answers (1)

Gaurav Garg
Gaurav Garg on 12 Jun 2020
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.

  0 Comments

Sign in to comment.


Translated by