Different size error bar caps

조회 수: 6(최근 30일)
Lorcan Conlon
Lorcan Conlon 2020년 9월 27일
댓글: Lorcan Conlon 2020년 9월 30일
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!!

채택된 답변

Prudhvi Peddagoni
Prudhvi Peddagoni 2020년 9월 30일
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
Lorcan Conlon
Lorcan Conlon 2020년 9월 30일
This is perfect Prudhvi, thank you!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

범주

Find more on Line Plots 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!

Translated by