plotting side-by-side error bars and data points
5 views (last 30 days)
Show older comments
Hello Experts,
I would be much grateful if you could help me with the following matter. I would like to create a plot similar to the one shown below.
Specifically, I would like to have the following features:
- Data points should be displayed as purple solid cirles (the shape, colors don't matter) and they are connected by line segments.
- Next to each data point, I want to place 2 error bars, side-by-side. Each error bar should have lower and upper bounds, as well as the center point.
- In terms of labelling, each data point and the 2 side-by-side error bars should be grouped together with a single label.
Here is my code, but it does not produce the desired plot.
hold on
x = 1:1:4;
xlim([0 5])
ylim([0 5])
data_points = [4 3 2 3];
plot(x,data_points,'s','LineWidth',6)
xlabel('Conditions','FontSize',16)
xticklabels({'label 1','label 2','label 3','label 4'})
ax = gca;
ax.FontSize = 16;
title('ABC','FontSize',16)
% -------- error bar 1 ----------------
y = [3.5 3.2 1.7 2.5];
yneg = [0.1 0.2 0.1 0.3];
ypos = [0.2 0.1 0.3 0.1];
e = errorbar(x,y,yneg,ypos,'*','MarkerSize',10);
e.LineWidth = 2;
% -------- error bar 2 ----------------
y = [3.1 2.7 2.1 3.2];
yneg = [0.1 0.2 0.1 0.3];
ypos = [0.2 0.1 0.3 0.1];
e = errorbar(x,y,yneg,ypos,'*','MarkerSize',10);
e.LineWidth = 2;
hold off
0 Comments
Accepted Answer
Dyuman Joshi
on 27 Mar 2023
Edited: Dyuman Joshi
on 27 Mar 2023
To get error bars on either side of data points, modify their x-coordinates using a threshold.
However, the plot obtained is not exactly the same as the image above, as the per data in hand.
hold on
x = 1:1:4;
xlim([0 5])
ylim([1.5 4.5])
data_points = [4 3 2 3];
%Plotting data points with line and marker
plot(x,data_points,'m-o','MarkerFaceColor', 'm')
xlabel('Conditions','FontSize',12)
%Defining xticks according to the data
xticks(x)
xticklabels({'label 1','label 2','label 3','label 4'})
title('ABC','FontSize',16)
%threshold
th = 0.075;
% -------- error bar 1 ----------------
y = [3.5 3.2 1.7 2.5];
yneg = [0.1 0.2 0.1 0.3];
ypos = [0.2 0.1 0.3 0.1];
%subtracting the threshold
errorbar(x-th,y,yneg,ypos,'*','MarkerSize',10,'LineWidth',2)
% -------- error bar 2 ----------------
y = [3.1 2.7 2.1 3.2];
yneg = [0.1 0.2 0.1 0.3];
ypos = [0.2 0.1 0.3 0.1];
%adding the threshold
errorbar(x+th,y,yneg,ypos,'*','MarkerSize',10,'LineWidth',2)
hold off
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!