Combine histograms in a bar plot

46 次查看(过去 30 天)
Rudolf
Rudolf 2021-5-7
编辑: Adam Danz 2021-5-11
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.

回答(1 个)

Adam Danz
Adam Danz 2021-5-7
编辑:Adam Danz 2021-5-11
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 个评论
Rudolf
Rudolf 2021-5-7
编辑:Rudolf 2021-5-7
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 2021-5-7
编辑:Adam Danz 2021-5-7
I see. You just need the "grouped" style.
hcx = histcounts(___);
hcy = histcounts(___);
bar([hcx(:),hcy(:)],'grouped')
(not tested, using my phone)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by