Combine histograms in a bar plot

21 ビュー (過去 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 Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by