Histogram - problem with X axis (shifting the data)

7 views (last 30 days)
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
Karolina on 11 Jan 2016
My data are rasters. A represents a full extent od data. B and C are parts of A raster - that I why I wrote that B and C are the sum of A - but I did not summed the values. I want to represent the histogram o counts for A, B, and C. The C on my histogram is higher for positive values.

Sign in to comment.

Accepted Answer

Guillaume on 11 Jan 2016
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);

More Answers (0)


Find more on Data Distribution 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