Borrar filtros
Borrar filtros

Filled error bars to a plot

14 visualizaciones (últimos 30 días)
Alexander
Alexander el 24 de Abr. de 2024
Editada: Voss el 24 de Abr. de 2024
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]')

Respuesta aceptada

Voss
Voss el 24 de Abr. de 2024
Editada: Voss el 24 de Abr. de 2024
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]')
  2 comentarios
Alexander
Alexander el 24 de Abr. de 2024
Editada: Alexander el 24 de Abr. de 2024
Thank you very much!!
Voss
Voss el 24 de Abr. de 2024
Editada: Voss el 24 de Abr. de 2024
You're welcome! Any questions, let me know.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by