Effacer les filtres
Effacer les filtres

plotting side-by-side error bars and data points

11 vues (au cours des 30 derniers jours)
Hung Dao
Hung Dao le 27 Mar 2023
Commenté : Dyuman Joshi le 27 Mar 2023
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

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 27 Mar 2023
Modifié(e) : Dyuman Joshi le 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
  2 commentaires
Hung Dao
Hung Dao le 27 Mar 2023
Thank you very much. This is exactly what I am looking for.
Dyuman Joshi
Dyuman Joshi le 27 Mar 2023
You are welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by