MATLAB: Histogram – problem with X axis (shifting the data)

histogramx axisxlim

I plotted a histogram which represent three data: A, B, C. The A represent the sum of B+C. I think there is something wrong with X axis (they are shifted for every data), because the C data for some values is higher than A. Does anyone know what can be wrong in my code?
% Generate histogram
h0=histogram(C,'BinLimits',[0.00,3.00],'FaceColor',[0.8 0.8 0.8],'EdgeColor','none','FaceAlpha',1);
hold on
h1=histogram(A,'BinLimits',[0.00,3.00],'FaceColor',[0 0 0],'EdgeColor','none','FaceAlpha',1);
hold on
h2=histogram(B,'BinLimits',[0.00,3.00],'FaceColor',[0.3 0.3 0.3],'EdgeColor','none','FaceAlpha',0.7);
hold on
xlim([0.1,1.4]);

Best Answer

My guess is that since you leave histogram to choose its own bins, your histograms don't have exactly the same bins. You can check the bins with:
h0.BinEdges %it would make more sense to call h0: hC
h1.BinEdges %it would make more sense to call h1: hA
h2.BinEdges %it would make a lot more sense to call h2: hB
If that is indeed the problem, either fix the bins to start with, or reuse the bins of the first histogram in the seconds and thid:
hC = histogram(C, 'BinLimits',[0.00,3.00],'FaceColor',[0.8 0.8 0.8],'EdgeColor','none','FaceAlpha',1);
hold on
hA = histogram(A, hc.BinEdges, 'BinLimits',[0.00,3.00],'FaceColor',[0 0 0],'EdgeColor','none','FaceAlpha',1);
hB = histogram(B, hc.BinEdges, 'BinLimits',[0.00,3.00],'FaceColor',[0.3 0.3 0.3],'EdgeColor','none','FaceAlpha',0.7);