Plotting confidence interval with bar plot

조회 수: 9 (최근 30일)
Amy Hassett
Amy Hassett 2020년 6월 6일
답변: Amy Hassett 2020년 6월 16일
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
Gaurav Garg
Gaurav Garg 2020년 6월 9일
How are you plotting the black line and grey shaded area?
Amy Hassett
Amy Hassett 2020년 6월 10일
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;

댓글을 달려면 로그인하십시오.

채택된 답변

Amy Hassett
Amy Hassett 2020년 6월 16일
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;

추가 답변 (1개)

Gaurav Garg
Gaurav Garg 2020년 6월 12일
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.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by