MATLAB Answers

Different size error bar caps

5 views (last 30 days)
I wish to make a bar chart with 5 bars with error bars on only two of these bars. My current code is
bounds=categorical({'aa','bb','cc','dd','ee'});
vals=[1,2,3,4,5]
set(gca,'TickLabelInterpreter','latex','Fontsize',18);
ylabel('Variance','Fontsize',20)
%
% pause(2)
colors = [0 0 1;
1 0 0;
1 0 0;
0 1 0;
0 1 0];
alphas = [1,0.3,1,0.3,1];
b = gobjects(size(bounds));
cla()
hold on
for i =1:numel(bounds)
b(i)=bar(bounds(i),vals(i));
if rem(i,2)==0
line='--';
else
line='-';
end
set(b(i),'FaceColor',colors(i,:),'FaceAlpha',alphas(i),'LineStyle',line);
end
ehigh=[0,0.5,0,0.8,0]
elow=ehigh;
hold on
er = errorbar(bounds,vals,elow,ehigh,'k','LineStyle','none');
er.Color = [0 0 0];
er.LineStyle = 'none';
er.LineWidth=1;
er(1).CapSize=20;
which almost does what I want, however I can still see the zero error bar in the three bars with no error. I can get rid of this by setting the capsize to 0 but I don't really want this as then I have no caps on the error bars that I do want. Is there a way to set the error bar cap sizes differently for each point?
Or equally good would be a way to only have error bars on certain bars.
Many thanks!!

Accepted Answer

Prudhvi Peddagoni
Prudhvi Peddagoni on 30 Sep 2020
Hi Lorcan,
You can set the linewidth for both bar and error bar to 1. So that you won’t see the thickness on the bars that you don’t want to see the error bars on.
Other thing to do is to declare another set of bounds and vals variables so that you can specify error bars for only those two bars like this.
ehigh = [0.5,0.8];
elow = ehigh;
hold on;
bound = [bounds(2) bounds(4)];
values = [vals(2) vals(4)];
er = errorbar(bound, values, elow, ehigh, 'k', 'LineStyle', 'none');
Hope this helps.
  1 Comment
Lorcan Conlon
Lorcan Conlon on 30 Sep 2020
This is perfect Prudhvi, thank you!

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!