Filled error bars to a plot (2024)

22 views (last 30 days)

Show older comments

Alexander on 24 Apr 2024 at 16:06

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot

Edited: Voss on 24 Apr 2024 at 17:26

Accepted Answer: Voss

  • growthWT2.xlsx

Open in MATLAB Online

Hello,

I have been trying to create some error bars in my MATLAB script. However, I think there are some problems. I have made a figure with three plots, and I wish to have "filled" error bars for the DO and pH. Additionally, the error bars for biomass have lines between the points, which I don't know how to remove. It would be nice if only the error bars were present.

I have inserted the code to my plot and data. Excel file is attached aswell.

growth = readtable("growthWT2.xlsx")

Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.

growth = 167x15 table

Time_s_ Time_h_ O21 O22 O23 MeanO2 pH1 pH2 pH3 meanPH Time_h__1 biomass1 biomass2 biomass3 MeanBio ________ __________ ______ ______ ______ ______ ______ ______ ______ ______ __________ ________ ________ ________ _______ 0.016667 0.00027778 96.27 95.987 97.441 96.566 7.1426 7.1451 7.1513 7.1463 0.00027778 0.2268 0.2268 0.2268 0.2268 2.0167 0.033611 97.081 96.795 98.079 97.318 7.1437 7.1419 7.1533 7.1463 1.0022 0.3888 0.3888 0.3888 0.3888 4.0181 0.066968 97.308 97.221 97.494 97.341 7.1432 7.1404 7.1493 7.1443 2.019 0.648 0.684 0.648 0.66 6.0245 0.10041 97.855 97.322 98.725 97.967 7.143 7.1476 7.1544 7.1483 3.0364 1.152 1.152 1.152 1.152 8.0249 0.13375 97.88 97.83 98.239 97.983 7.1425 7.151 7.1519 7.1485 4.0271 1.872 1.8 1.8 1.824 10.033 0.16722 98.186 97.79 98.525 98.167 7.1375 7.1414 7.1472 7.142 5.0078 2.88 3.096 3.024 3 12.033 0.20056 97.945 98.183 98.139 98.089 7.1409 7.1454 7.1546 7.147 6.0765 2.88 2.808 2.952 2.88 14.033 0.23389 97.819 97.924 98.216 97.986 7.142 7.1469 7.1535 7.1475 NaN NaN NaN NaN NaN 16.037 0.26729 97.274 97.864 98.266 97.801 7.1402 7.148 7.1533 7.1472 NaN NaN NaN NaN NaN 18.05 0.30083 97.123 98.062 97.506 97.564 7.1395 7.147 7.1457 7.1441 NaN NaN NaN NaN NaN 20.052 0.33419 97.303 97.832 98.004 97.713 7.1389 7.1457 7.1473 7.1439 NaN NaN NaN NaN NaN 22.057 0.36762 97.603 97.078 97.568 97.416 7.1275 7.1453 7.1441 7.139 NaN NaN NaN NaN NaN 24.067 0.40111 96.798 97.353 97.74 97.297 7.1311 7.1367 7.1431 7.1369 NaN NaN NaN NaN NaN 26.067 0.43444 96.964 97.436 97.395 97.265 7.1332 7.1354 7.1466 7.1384 NaN NaN NaN NaN NaN 28.072 0.46787 96.634 97.466 97.323 97.141 7.1336 7.1385 7.15 7.1407 NaN NaN NaN NaN NaN 30.083 0.50139 96.133 96.895 96.742 96.59 7.1266 7.1438 7.1486 7.1396 NaN NaN NaN NaN NaN

time = [growth.Time_h_];

timeOD = [growth.Time_h__1];

DO_1 = [growth.O21];

DO_2 = [growth.O22];

DO_3 = [growth.O23];

mean_DO = [growth.MeanO2];

S_DO = [DO_1 DO_2 DO_3];

stand_DO = std(S_DO,0,2);

pH_1 = [growth.pH1];

pH_2 = [growth.pH2];

pH_3 = [growth.pH3];

mean_pH = [growth.meanPH];

S_pH = [pH_1 pH_2 pH_3];

stand_pH = std(S_pH,0,2);

Bio_1 = [growth.biomass1];

Bio_2 = [growth.biomass2];

Bio_3 = [growth.biomass3];

mean_bio = [growth.MeanBio];

S_bio = [Bio_1 Bio_2 Bio_3];

stand_bio = std(S_bio,0,2);

% mean values

t = tiledlayout(2,1);

nexttile

yyaxis left

fill([time, flip(time)], [mean_DO+stand_DO, flip(mean_DO-stand_DO)], 'b', 'FaceAlpha', 0.3, 'EdgeColor', 'none')

hold on

plot(time, mean_DO, 'b')

ylabel('DO')

yyaxis right

fill([time, flip(time)], [mean_pH+stand_pH, flip(mean_pH-stand_pH)], 'r', 'FaceAlpha', 0.3, 'EdgeColor', 'none')

hold on

plot(time,mean_pH, 'r')

ylabel('pH')

legend('DO', 'DO Std Dev' ,'pH', 'pH Std dev')

nexttile

plot(timeOD,mean_bio,'o')

hold on

errorbar(timeOD, mean_bio, stand_bio)

ylabel('Biomass [g/L]')

legend('Biomass', 'Biomass Std Dev')

title(t,'Growth parameters of PR01 average value')

xlabel(t,'Time [h]')

Filled error bars to a plot (2)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Voss on 24 Apr 2024 at 16:18

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#answer_1447231

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#answer_1447231

Edited: Voss on 24 Apr 2024 at 16:29

Open in MATLAB Online

  • growthWT2.xlsx

growth = readtable("growthWT2.xlsx");

Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.

time = growth.Time_h_; % no [ ] required

timeOD = growth.Time_h__1;

DO_1 = growth.O21;

DO_2 = growth.O22;

DO_3 = growth.O23;

mean_DO = growth.MeanO2;

S_DO = [DO_1 DO_2 DO_3];

stand_DO = std(S_DO,0,2);

pH_1 = growth.pH1;

pH_2 = growth.pH2;

pH_3 = growth.pH3;

mean_pH = growth.meanPH;

S_pH = [pH_1 pH_2 pH_3];

stand_pH = std(S_pH,0,2);

Bio_1 = growth.biomass1;

Bio_2 = growth.biomass2;

Bio_3 = growth.biomass3;

mean_bio = growth.MeanBio;

S_bio = [Bio_1 Bio_2 Bio_3];

stand_bio = std(S_bio,0,2);

% mean values

t = tiledlayout(2,1);

nexttile

yyaxis left

% use vertical concatenation [;] instead of horizontal [,] since the

% vectors are column vectors. otherwise, you get two patches per fill()

% call.

% also, capture the object returned by each fill() and patch() call, for

% specifying the order in the legend correctly

pDO = fill([time; flip(time)], [mean_DO+stand_DO; flip(mean_DO-stand_DO)], 'b', 'FaceAlpha', 0.3, 'EdgeColor', 'none');

hold on

lDO = plot(time, mean_DO, 'b');

ylabel('DO')

yyaxis right

pPH = fill([time; flip(time)], [mean_pH+stand_pH; flip(mean_pH-stand_pH)], 'r', 'FaceAlpha', 0.3, 'EdgeColor', 'none');

hold on

lPH = plot(time,mean_pH, 'r');

ylabel('pH')

% specify the order of the objects (lines and patches) for the legend

legend([lDO pDO lPH pPH],{'DO', 'DO Std Dev' ,'pH', 'pH Std dev'})

nexttile

plot(timeOD,mean_bio,'o')

hold on

% specify no line for the errorbar

errorbar(timeOD, mean_bio, stand_bio, 'LineStyle','none')

ylabel('Biomass [g/L]')

legend('Biomass', 'Biomass Std Dev')

title(t,'Growth parameters of PR01 average value')

xlabel(t,'Time [h]')

Filled error bars to a plot (4)

2 Comments

Show NoneHide None

Alexander on 24 Apr 2024 at 16:43

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#comment_3142446

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#comment_3142446

Edited: Alexander on 24 Apr 2024 at 16:56

Thank you very much!!

Voss on 24 Apr 2024 at 16:56

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#comment_3142476

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2111326-filled-error-bars-to-a-plot#comment_3142476

Edited: Voss on 24 Apr 2024 at 17:26

You're welcome! Any questions, let me know.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsLine PlotsErrorbars

Find more on Errorbars in Help Center and File Exchange

Tags

  • filled error bars
  • plot

Products

  • MATLAB

Release

R2022b

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.


Filled error bars to a plot (7)

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Filled error bars to a plot (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5900

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.