1
votes

I'm plotting two frequency bar graphs on top of each other and I would like to make one of the bar graphs transparent. I've tried this:

[n,xout] = hist(data1,10); %Use 10 bins for the histogram
B1 = bar(xout,n/sum(n),'b'); %relative frequency is n/sum(n)
hold on
[n,xout] = hist(data2,10); %Use 10 bins for the histogram
B2 = bar(xout,n/sum(n),'r'); %relative frequency is n/sum(n)
ch = get(B2,'child');
set(ch,'facea',.4)

However, the second plot graphed does not seem to be transparent. Is there something I'm missing?

Edit

output of get(B2):

Annotation: [1x1 matlab.graphics.eventdata.Annotation]
             BarLayout: 'grouped'
              BarWidth: 0.8000
              BaseLine: [1x1 Baseline]
             BaseValue: 0
          BeingDeleted: 'off'
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: []
              Clipping: 'on'
             CreateFcn: ''
             DeleteFcn: ''
           DisplayName: ''
             EdgeColor: [0 0 0]
             FaceColor: 'flat'
      HandleVisibility: 'on'
               HitTest: 'on'
            Horizontal: 'off'
         Interruptible: 'on'
             LineStyle: '-'
             LineWidth: 0.5000
                Parent: [1x1 Axes]
         PickableParts: 'visible'
              Selected: 'off'
    SelectionHighlight: 'on'
          ShowBaseLine: 'on'
                   Tag: ''
                  Type: 'bar'
         UIContextMenu: []
              UserData: []
               Visible: 'on'
                 XData: [1x10 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [0.5333 0.1778 0.0444 0.0889 0.0889 0.0222 0 0.0222 0 0.0222]
           YDataSource: ''
1

1 Answers

2
votes

That's because B2 does not have any children so ch is empty (at least in R2015B).

You can change the FaceAlpha property directly using for example

B2 = bar(xout,n/sum(n),'r','facea',.4);

instead of looking for its children.