필터 지우기
필터 지우기

plotting side-by-side error bars and data points

조회 수: 11 (최근 30일)
Hung Dao
Hung Dao 2023년 3월 27일
댓글: Dyuman Joshi 2023년 3월 27일
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:
  1. Data points should be displayed as purple solid cirles (the shape, colors don't matter) and they are connected by line segments.
  2. 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.
  3. 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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 3월 27일
편집: Dyuman Joshi 2023년 3월 27일
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
  댓글 수: 2
Hung Dao
Hung Dao 2023년 3월 27일
Thank you very much. This is exactly what I am looking for.
Dyuman Joshi
Dyuman Joshi 2023년 3월 27일
You are welcome!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by