Filled error bars to a plot
15 views (last 30 days)
Show older comments
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")
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]')
0 Comments
Accepted Answer
Voss
on 24 Apr 2024
Edited: Voss
on 24 Apr 2024
growth = readtable("growthWT2.xlsx");
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]')
2 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!