Combine histograms in a bar plot

조회 수: 48 (최근 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