need help for adding a error bar

2 views (last 30 days)
surawut.A
surawut.A on 7 Apr 2022
Commented: Dave B on 7 Apr 2022
%------------------------------------------------------
M = [15, 0.765891, 30, 0.466818, 45, 0.770541
15, 0.3472, 30, 0.233845234, 45, 0.15337401
15, 0.15, 30, 0.261, 45, 0.344
15, 0.161, 30, 0.42, 45, 0.724];
b = bar(M(:,[2 4 6])');
set(gca, 'XTickLabel', string(M(1,[1 3 5])));
xlabel('%Carbon monoxide content');
ylabel('Mass gain (μg/mm^2)');
legend({['900' char(176) 'C'], ['800' char(176) 'C'], ['700' char(176) 'C'], ['600' char(176) 'C']});
%------------------------------------------------------
% add error bars
%
%------------------------------------------------------
hold on;
xx = [b(1).XEndPoints; b(2).XEndPoints; b(3).XEndPoints; b(4).XEndPoints];
yy = [b(1).YData; b(2).YData; b(3).YData; b(4).YData];
yneg = [0.51560 0.25806 0.12860 0.12847 0.28987 0.16209 0.12867 0.12913 0.29282 0.12890 0.12853 0.12920];%%%%%%%%%%
ypos = [0.77200 0.38820 0.19300 0.22494 0.61180 0.32200 0.20403 0.33897 0.55095 0.22610 0.19350 0.35329];%%%%%%%%%%
eb = errorbar(xx,yy,yneg,ypos'o', 'linestyle', 'none', 'color', 'k', 'linewidth', .8);
set(eb, 'HandleVisibility', 'off');
I would like to add a error bar to my graph. If you run a bar code you will get a 12 bar with 4 temperature and 3 different %carbon content,
I have a number from calulation on yneg (ErrorMin) ypos (ErrorMax),
I arange like first number from neg and pos are for first bar and continue.
and if you know how to do a pattern fill on a bar like a pircture below please help

Accepted Answer

Dave B
Dave B on 7 Apr 2022
I think the problem you ran into was that you were calling errorbar with 4x4 matrices for x and y, and 1x12 vectors for yneg and ypos. If you provide vectors for x and y things will work well, and it's easy to convert your matrices to vectors with (:) - just make sure that the values match up as you expect (i.e. that you don't need a transpose).
In the snippet below the only line of code I adjusted was the call to errorbar, where I inserted (:) after xx and yy (and a missing comma before 'o'
clf
M = [15, 0.765891, 30, 0.466818, 45, 0.770541
15, 0.3472, 30, 0.233845234, 45, 0.15337401
15, 0.15, 30, 0.261, 45, 0.344
15, 0.161, 30, 0.42, 45, 0.724];
b = bar(M(:,[2 4 6])');
set(gca, 'XTickLabel', string(M(1,[1 3 5])));
xlabel('%Carbon monoxide content');
ylabel('Mass gain (μg/mm^2)');
legend({['900' char(176) 'C'], ['800' char(176) 'C'], ['700' char(176) 'C'], ['600' char(176) 'C']});
hold on;
xx = [b(1).XEndPoints; b(2).XEndPoints; b(3).XEndPoints; b(4).XEndPoints];
yy = [b(1).YData; b(2).YData; b(3).YData; b(4).YData];
yneg = [0.51560 0.25806 0.12860 0.12847 0.28987 0.16209 0.12867 0.12913 0.29282 0.12890 0.12853 0.12920];%%%%%%%%%%
ypos = [0.77200 0.38820 0.19300 0.22494 0.61180 0.32200 0.20403 0.33897 0.55095 0.22610 0.19350 0.35329];%%%%%%%%%%
eb = errorbar(xx(:),yy(:),yneg,ypos, 'o', 'linestyle', 'none', 'color', 'k', 'linewidth', .8);
set(eb, 'HandleVisibility', 'off');
  2 Comments
Dave B
Dave B on 7 Apr 2022
the numbers for yneg and ypos are how far up and down the errorbars go from the bars...maybe you can expand a bit on what's seems like it doesn't use the values you're specifying?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by