Plotting error bars on curve (2024)

20 views (last 30 days)

Show older comments

alexandra ligeti about 22 hours ago

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve

Commented: Star Strider about 20 hours ago

Open in MATLAB Online

Hello,

I have code that compares two devices and I am plotting the error on y-axis throughout a gait cycle % (on the x axis).

I am trying to get the error bars to plot the standard error but I am having an unexpected result when plotting the curve.

This is the code I am working with.

Initially x, ave_diff and SE are 1X1000 and I would like to plot it over a gait cycle from 0-100% hence why I have resampled the signals.

However, when I run this code I have extra values at the end of the plot, yet the resmapled variables are all 101X1?

I cannot spot my error and was wondering if someone my point me in the correct direction or suggest where my fault lies.

I have attached the curve and the area where the issue lies, you can see the standard error bars also become strange.

Thanks in advance

figure()

x = smooth(resample((gc),101,length(gc),'Dimension',1));

ave_diff_resample = smooth(resample((ave_diff),101,length(ave_diff),'Dimension',1));

SE_resampled = smooth(resample((SE),101,length(SE),'Dimension',1));

set(gcf,'Color','w');

%standard error plot

plot(x, ave_diff_resample,'-','Color',[0 0 0],'linewidth',0.5)

hold on

% Adding error bars

errorbar(x, ave_diff_resample, SE_resampled, 'vertical');

hold on

hline = refline(0, 0);

hline.Color = 'k';

xlim([0 100]);

ylim([-15 15]);

legend('Signed Difference', 'Standard error bar')

xlabel('Gait cycle %')

ylabel('Difference (deg)')

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Star Strider about 20 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#answer_1474861

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#answer_1474861

Open in MATLAB Online

  • errror curve.png
  • error.png

I really can’t do anything without the data. The rest of the code (that imports the data and processes it including calculating the standard errors) would be helpful as well. (An image may be worth a thousand words, however the data files and code are worth a thousand images on MATLAB Answers.)

pngs = dir('*.png');

for k = 1:numel(pngs)

figure

imshow(imread(pngs(k).name))

end

Plotting error bars on curve (3)

Plotting error bars on curve (4)

.

4 Comments

Show 2 older commentsHide 2 older comments

alexandra ligeti about 20 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191561

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191561

Open in MATLAB Online

Ok perfect will attach the full script, I have used my daily limit so cannot attach the data, will attach the data tomorrow.

Thank you :)

clear all

%File location of Results to analyze

load ('C:\Users\lexil\Documents\Patient_Study\Healthy Participants\Participant_data/T07b.mat')%young

%If subjects need to be excluded post hoc, you can list the number here

omits = [];

alphaVicon_GC=[];

alpha_IMU=[];

Vicon_GC=[];

IMU_GC=[];

minV = [];

minIMU = [];

maxV = [];

maxIMU = [];

ROMV = [];

ROMRD = [];

RMSE_IMU = [];

p = [];

diff = [];

std_diff = [];

SE = [];

% dat_store{c,1} = gc; %Gait cycle 0-100

% dat_store{c,2} = alphaVicon_GC; %Vicon

% dat_store{c,3} = alpha1D_GC; %RD

% dat_store{c,4} = alpha_MS_GC;

% dat_store{c,5} = ROM;

% dat_store{c,6} = RMSE;

j=0;

sz = size(dat_store);

for i = 1:sz(1)-length(omits) %

if i == omits

else

ROM_all = dat_store{i,4};

ROMIMU_diff(i,:) = mean(ROM_all(:,1:3))-mean(ROM_all(:,4:6));

ROMIMU(i,:) = mean(ROM_all(:,4:6));

ROMVic(i,:) = mean(ROM_all(:,1:3));

alphaVicon_GC = [alphaVicon_GC;(dat_store{i,2})];

%alpha1D_GC = [alpha1D_GC;dat_store{i,3}];

alpha_IMU = [alpha_IMU;dat_store{i,3}];

% Average gc for each individual

Vicon_GC = [Vicon_GC;mean(dat_store{i,2})];

IMU_GC = [IMU_GC;mean(dat_store{i,3})];

RMSE = dat_store{i,5};

%RMSE_1D = [RMSE_1D ;RMSE(2,:)];

RMSE_IMU = [RMSE_IMU ;RMSE(1,:)];

%CORR_1D(i) = corr(reshape(dat_store{i,2},[],1),reshape(dat_store{i,3},[],1));

CORR_IMU(i) = corr(reshape(dat_store{i,2},[],1),reshape(dat_store{i,3},[],1));

end

end

Vicon_GC = Vicon_GC';

IMU_GC = IMU_GC';

diff = Vicon_GC - IMU_GC;

ave_diff = mean(Vicon_GC') - mean(IMU_GC');

std_diff = std(diff,0,2);

SE = std_diff/sqrt(i);

var = ["alphaIMU"];

RMSEtable = [mean(mean(RMSE_IMU,2)) std(mean(RMSE_IMU,2))];

table(var,RMSEtable)

var2 = ["alpha"];

CORR = [mean(CORR_IMU) std(CORR_IMU)];

table(var2,CORR)

GC_all_IMU = reshape(alphaVicon_GC,[],1)-reshape(alpha_IMU,[],1);

GC_all_IMU(abs(GC_all_IMU)<30);

RMSE_IMUa = sqrt(mean(GC_all_IMU.^2))

%ROM

minV = min(ROM_all(:,1));

minIMU = min(ROM_all(:,4));

maxV = max(ROM_all(:,2));

maxIMU = max(ROM_all(:,5));

minstd_V = std(ROM_all(:,1));

minstd_IMU = std(ROM_all(:,4));

maxstd_V = std(ROM_all(:,2));

maxstd_IMU = std(ROM_all(:,5));

ROMstd_V = std(ROM_all(:,3));

ROMstd_IMU = std(ROM_all(:,6));

ROMV = maxV - minV;

ROMRD = maxIMU - minIMU;

gc = dat_store{1,1};

sig = 1.96; %95% Confidence intervals but if sd then replace with 1

figure

patch([gc,flip(gc)],[mean(alphaVicon_GC)-sig*std(alphaVicon_GC) flip(mean(alphaVicon_GC)+sig*std(alphaVicon_GC)) ],[0 0 0],'facealpha',0.2,'edgealpha',0)

hold on

patch([gc,flip(gc)],[mean(alpha_IMU)-sig*std(alpha_IMU) flip(mean(alpha_IMU)+sig*std(alpha_IMU)) ],[1 0 0],'facealpha',0.1,'edgealpha',0)

%patch([gc,flip(gc)],[mean(alpha1D_GC)-sig*std(alpha1D_GC) flip(mean(alpha1D_GC)+sig*std(alpha1D_GC)) ],[0 0 1],'facealpha',0.1,'edgealpha',0)

plot(gc ,mean(alpha_IMU),'r--','LineWidth',1)

%plot(gc ,mean(alpha1D_GC),'b:','LineWidth',1)

plot(gc, mean(alphaVicon_GC),'k-','LineWidth',1)

%xlim([0 TimeEnd])

ylim([-20 80])

grid on

ylabel('Knee flexion angle (\circ)')

xlabel('Gait Cycle (%)')

xticklabels(["0%","20%","40%","60%","80%","100%"])

% g(1) = patch(NaN,NaN,[1 0 0],'facealpha',0.2);

% g(2) = patch(NaN,NaN,[0 0 0],'facealpha',0.2);

% g(3) = patch(NaN,NaN,[0 0 1],'facealpha',0.2);

g(1) = plot(NaN,NaN,'k-','LineWidth',1);

g(2) = plot(NaN,NaN,'r--','LineWidth',1);

rgb = [0 0 0];

FaceAlpha = (0.1);

g(3) = patch([NaN],[NaN],rgb,'EdgeAlpha', 0, 'FaceAlpha',FaceAlpha);

legend(g,'Camera-Marker','IMU','+/- 1.96*SD','location','northwest')

figure()

subplot(3,1,1)

plot(gc ,mean(alpha_IMU),'k--','LineWidth',1)

hold on

plot(gc, mean(alphaVicon_GC),'k-','LineWidth',1)

legend('IMU','Vicon')

xlabel('Gait cycle %')

ylabel('Knee Angle (deg)')

ylim([-20 80]);

grid on

subplot(3,1,2)

plot(gc, (mean(alphaVicon_GC)-mean(alpha_IMU)),'k-','LineWidth',1)

legend('Signed difference')

xlabel('Gait cycle %')

ylabel('Difference (deg)')

ylim([-10 10]);

grid on

subplot(3,1,3)

plot(gc, abs(mean(alphaVicon_GC)-mean(alpha_IMU)),'k-','LineWidth',1)

legend('Absolute difference')

xlabel('Gait cycle %')

ylabel('Difference (deg)')

ylim([-10 10]);

grid on

figure()

x = smooth(resample((gc),101,length(gc),'Dimension',1));

ave_diff_resample = smooth(resample((ave_diff),101,length(ave_diff),'Dimension',1));

SE_resampled = smooth(resample((SE),101,length(SE),'Dimension',1));

set(gcf,'Color','w');

%standard error plot

plot(x, ave_diff_resample,'-','Color',[0 0 0],'linewidth',0.5)

hold on

% Adding error bars

errorbar(x, ave_diff_resample, SE_resampled, 'vertical');

hold on

hline = refline(0, 0);

hline.Color = 'k';

xlim([0 100]);

ylim([-15 15]);

legend('Signed Difference', 'Standard error bar')

xlabel('Gait cycle %')

ylabel('Difference (deg)')

%% Average Bland-Altman plot

% Plot mean difference line

figure()

set(gcf,'Color','w');

ave_vic = mean(Vicon_GC');

ave_imu = mean(IMU_GC');

mean_data = (ave_vic + ave_imu)/2;

mean_data = resample((mean_data),101,length(mean_data),'Dimension',1);

std_diff = resample((std_diff),101,length(std_diff),'Dimension',1);

first_index = 1;

last_index = numel(mean_data);

scatter(mean_data(first_index), ave_diff_resample(first_index),80, '*');

hold on

scatter(mean_data(last_index), ave_diff_resample(last_index),80, '^');

hold on

scatter(mean_data(2:100), ave_diff_resample(2:100), 'filled','MarkerFaceColor', 'black');

hold on;

%+- 2SD

plot([0 120], [mean(ave_diff_resample)'+ 2*mean(std_diff), mean(ave_diff_resample)' + 2*mean(std_diff)], 'r--');

plot([0 120], [mean(ave_diff_resample)' - 2*mean(std_diff), mean(ave_diff_resample)' - 2*mean(std_diff)], 'r--');

%mean diff line

plot([0 120], [mean(ave_diff_resample), mean(ave_diff_resample)], 'k-', 'LineWidth', 1);

%calculate error bars

%error bars are the standard error of the sample

error_bar_y = errorbar(mean_data,ave_diff_resample,SE_resampled, 'vertical', 'LineStyle', 'none');

set(error_bar_y, 'color', 'k', 'LineWidth', 0.5)

legend({'HS', 'HS', 'Average gait cycle Data', '+ 2 SD', '- 2 SD', 'Mean Difference', 'SE'});

hold on

xlim([0 120])

ylim([-10 10])

xlabel('Mean($\o - \bar{\o}$) (deg)', 'Interpreter','Latex')

ylabel('Difference (deg)')

title('Cycling - Younger Adults')

Star Strider about 20 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191596

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191596

Thank you for posting the code.

I really need the data to see what the problem is. We will have to wait until tomorrow.

Alternatively, if you have exhausted your daily limit, did you upload the file to another post? If so, please post the URL for that post.

alexandra ligeti about 20 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191606

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191606

I have not posted it to another previous post as this is new data sadly, will post it first thing tomorrow.

Thanks

Star Strider about 19 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191686

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2130421-plotting-error-bars-on-curve#comment_3191686

My pleasure!

I will be looking for it.

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • error bars
  • curve plotting
  • resample data
  • plot

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Plotting error bars on curve (9)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Plotting error bars on curve (2024)

References

Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 5974

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.