connecting each data points in a bar graph with an error bar

5 views (last 30 days)
Hi, I would like to connect those two corresponding points from one bar graph to another (first dot to another first dot).
I have a code like this now to give a bar graph and an error and each individual data points. Could help how I can connect points? thanks.
Saline1 = [41 55];
Saline2 = [34 26];
bar(1,mean(Saline1), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
bar(2,mean(Saline2), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
plot(1,Saline1,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
plot(2,Saline2,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
errorbar(1,mean(Saline1),(std(Saline1)/sqrt(size(Saline1,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0])
hold on
errorbar(2,mean(Saline2),(std(Saline2)/sqrt(size(Saline2,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0])
hold on

Accepted Answer

Star Strider
Star Strider on 15 Mar 2024
Sure!
The easiest way is probably to return the handles of the errorbar calls to get the ‘XData’, ‘YData’, ‘’YPOsitiveDelta’, and ‘YNegativeDelta’ values, and use those to plot the lines.
Try this —
Saline1 = [41 55];
Saline2 = [34 26];
bar(1,mean(Saline1), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
bar(2,mean(Saline2), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
plot(1,Saline1,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
plot(2,Saline2,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
heb1 = errorbar(1,mean(Saline1),(std(Saline1)/sqrt(size(Saline1,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0]);
% get(heb1)
hold on
heb2 = errorbar(2,mean(Saline2),(std(Saline2)/sqrt(size(Saline2,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0]);
plot([heb1.XData heb2.XData], [heb1.YData+heb1.YPositiveDelta heb2.YData+heb2.YPositiveDelta], '-r', 'LineWidth',2)
plot([heb1.XData heb2.XData], [heb1.YData-heb1.YNegativeDelta heb2.YData-heb2.YNegativeDelta], '-g', 'LineWidth',2)
hold off
.

More Answers (1)

Chunru
Chunru on 15 Mar 2024
Saline1 = [41 55];
Saline2 = [34 26];
bar(1,mean(Saline1), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
bar(2,mean(Saline2), 0.6, 'FaceColor',[0.7 0.7 0.7])
hold on
plot(1,Saline1,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
plot(2,Saline2,'--o','MarkerSize',10,'MarkerFaceColor',...
[0.3 0.3 0.3],'MarkerEdgeColor','none','Color',[0.3 0.3 0.3],'LineWidth',2)
hold on
e(1) = errorbar(1,mean(Saline1),(std(Saline1)/sqrt(size(Saline1,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0])
e =
ErrorBar with properties: Color: [0 0 0] LineStyle: 'none' LineWidth: 2 Marker: 'none' XData: 1 YData: 48 XNegativeDelta: [1x0 double] XPositiveDelta: [1x0 double] YNegativeDelta: 7.0000 YPositiveDelta: 7.0000 Use GET to show all properties
hold on
e(2) = errorbar(2,mean(Saline2),(std(Saline2)/sqrt(size(Saline2,2))),...
'LineStyle','none','LineWidth',2,'Color',[0 0 0])
e =
1x2 ErrorBar array: ErrorBar ErrorBar
hold on
%% link the positive delta
% Get the coordinates
x = [e.XData];
y = [e.YData] + [e.YPositiveDelta];
plot(x, y, 'b-')

Tags

Community Treasure Hunt

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

Start Hunting!