Does anyone knows how to stack 2 histogram. I have certain area data for 20 years. I am calculating yearly number of events by hist command. And also I have another area data for 20 years. Doing the same, calculating yearly number of events by hist command for the area also. Note for 2 data time the same, 1980-2000 in years.
Now I wanted 2 hist data stack. Is there any way to do it?
Thanks!

4 件のコメント

José-Luis
José-Luis 2016 年 6 月 30 日
What do you mean by stack?
davit petraasya
davit petraasya 2016 年 6 月 30 日
編集済み: davit petraasya 2016 年 6 月 30 日
adding y values, y1 and y2 values. Notice they are different size of vectors.
José-Luis
José-Luis 2016 年 6 月 30 日
I still don't get it. Could you show a figure as an example? And some data?
Stephen Licata
Stephen Licata 2020 年 12 月 23 日
編集済み: Stephen Licata 2020 年 12 月 23 日
That is a very good and fun example - many thanks!
BTW, if you want a special color, like 'gray' (which does not have a Matlab shortcut symbol code), do this bar command
bar(binrng,counts2,'FaceColor', [0.75 0.75 0.75])
which is equivalent to this version using the RGB integer values:
bar(binrng,counts2,'FaceColor', [192 192 192]/255)

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2016 年 6 月 30 日

6 投票

The hist function does not offer a 'stacked' option, but you can create the effect easily enough with the histc function and a bar plot.
Experiment with this to get the result you want with your data:
d1 = randi(9, 50, 1); % Create Data
d2 = randi(9, 50, 1); % Create Data
binrng = 1:9; % Create Bin Ranges
counts1 = histc(d1, binrng); % Histogram For ‘d1’
counts2 = histc(d2, binrng); % Histogram For ‘d2’
counts3 = counts1 + counts2; % Histogram Sum ‘d1’+‘d2’
figure(1)
bar(binrng, counts3, 'b')
hold on
bar(binrng, counts1, 'y')
hold off
legend('Data 1', 'Data 2')
The idea is straightforward: create histogram counts for both sets of data, add them, then use the bar plot to first plot the sum, then overplot with one of the others. That will create your stacked histogram plot.

11 件のコメント

davit petraasya
davit petraasya 2016 年 6 月 30 日
Thanks Strider, this is exactly what I needed. One thing more to ask when you impose binrng are you giving command the number of bins? If I want 20 bins it will be binrng=1:20 ?
Star Strider
Star Strider 2016 年 6 月 30 日
My pleasure.
‘If I want 20 bins it will be binrng=1:20 ?’
Yes. Just change the ‘binrng’ assignment to that, assign your data as ‘d1’ and ‘d2’ (duplicating the assignment for the purposes of plotting it), and you can run my code without any further changes, unless you also want to change the colours of the bars.
davit petraasya
davit petraasya 2016 年 7 月 8 日
Okay, Thanks Strider, It worked perfectly!
Star Strider
Star Strider 2016 年 7 月 8 日
My pleasure.
Wesser
Wesser 2021 年 6 月 8 日
I know this is an old question, but can you do something similar if d1 and d2 are different length vectors? I also want to stack histograms, but each histogram does not have the same number of data points.
Thanks!
Star Strider
Star Strider 2021 年 6 月 8 日
The lengths of the vectors should not matter, so long as they are both column vectors.
Example —
d1 = randi(9, 50, 1); % Create Data
d2 = randi(9, 90, 1); % Create Data
binrng = 1:9; % Create Bin Ranges
counts1 = histc(d1, binrng); % Histogram For ‘d1’
counts2 = histc(d2, binrng); % Histogram For ‘d2’
counts3 = counts1 + counts2; % Histogram Sum ‘d1’+‘d2’
figure(1)
bar(binrng, counts3, 'b')
hold on
bar(binrng, counts1, 'y')
hold off
legend('Data 1', 'Data 2')
.
Wesser
Wesser 2021 年 6 月 9 日
Thanks!
Star Strider
Star Strider 2021 年 6 月 9 日
My pleasure!
Jordan
Jordan 2021 年 7 月 15 日
Hi, I know this is an old question too. But what if there are 6 separate vectors I want stacked? I can ask it as a new question if you would prefer that. Thanks!
Star Strider
Star Strider 2021 年 7 月 15 日
That’s a bit ambiguous.
Perhaps:
v1 = randi(9,1,50);
v2 = randi(9,1,50);
v3 = randi(9,1,50);
v4 = randi(9,1,50);
v5 = randi(9,1,50);
v6 = randi(9,1,50);
vm = [v1; v2; v3; v4; v5; v6];
binrng = 1:9;
for k = 1:size(vm,1)
counts(k,:) = histc(vm(k,:), binrng);
end
figure
bar(binrng,counts,'stacked')
grid
Experiment, if necessary, to get different results.
.
Joris Bockhofer
Joris Bockhofer 2023 年 7 月 5 日
Make sure to use histc!
MatLab recommended using histcounts instead but this will not work because histcounts yields one less value than #bins for whatever reason.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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