Combine histograms in a bar plot

46 views (last 30 days)
Rudolf
Rudolf on 7 May 2021
Edited: Adam Danz on 11 May 2021
I'm using this code to represent my data as wanted:
histogram('BinEdges', 0:20, 'BinCounts',myArrayOfDouble,'LineWidth', 0.5)
but now i want to show several of these at the same figure, and the solution with figure made by user the cyclist in thread under accepted answer here is preferable:
But how? (I don't want the histogram function where data gets put in bins according value, but have to use code shown to get the figure as wanted.) Sorry for poor explaining. Hope someone knows a trick for this.

Answers (1)

Adam Danz
Adam Danz on 7 May 2021
Edited: Adam Danz on 11 May 2021
I think what you want is to produce a histogram with data that already contains the bin counts rather than using the histogram function to count the number of values within each bin.
If my interpretation is correct, use the syntax, histogram('BinEdges',edges,'BinCounts',counts)
Example
binEdges1 = [0 1 2 3 4 5]; % 5 bins, 6 edges
counts1 = [9 8 7 6 5]; % counts
binEdges2 = [2 3 4 5 6 7];
counts2 = [5 6 7 8 9];
figure()
hold on % <-- important
histogram('BinEdges',binEdges1,'BinCounts',counts1)
histogram('BinEdges',binEdges2,'BinCounts',counts2)
Or perhaps you want the grouped option with bar plots,
bar([hcx(:),hcy(:)],'grouped')
  2 Comments
Rudolf
Rudolf on 7 May 2021
Edited: Rudolf on 7 May 2021
Sorry for not explaining good enough. Guess if i take the picture from the other thread i may explain better.
I would like 3 datasets to be represented like this. But problem is that i've used histogram for each of them to get the data between x ticks.
% Code from the other thread, code which produced the figure
x = randn(2000,1);
y = 0.1 + randn(2000,1);
binRange = -3:0.5:3;
hcx = histcounts(x,[binRange Inf]);
hcy = histcounts(y,[binRange Inf]);
figure
bar(binRange,[hcx;hcy]')
(My question is still unclear or kind of impossible right?)
Adam Danz
Adam Danz on 7 May 2021
Edited: Adam Danz on 7 May 2021
I see. You just need the "grouped" style.
hcx = histcounts(___);
hcy = histcounts(___);
bar([hcx(:),hcy(:)],'grouped')
(not tested, using my phone)

Sign in to comment.

Categories

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!