How to get multiple groups plotted with histogram?
104 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Diemo Schwarz
il 7 Giu 2016
Modificato: the cyclist
il 7 Giu 2016
With hist, the columns of an input matrix would be interpreted as different data sets and the frequency bars grouped per bin:
hist(rand(20,4))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/165887/image.png)
How to get this behaviour with the new histogram function that is supposed to supercede hist?
histogram(rand(20,4))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/165888/image.png)
Risposta accettata
the cyclist
il 7 Giu 2016
Modificato: the cyclist
il 7 Giu 2016
As I suggested in this answer, I think the best you can do is to get the bin counts using the histcounts command, and then use the bar command to plot:
Here is an example.
rng 'default'
data1 = randn(20,1); data2 = randn(30,1); data3 = randn(40,1); data4 = randn(50,1);
edges = -4:1:4;
h1 = histcounts(data1,edges); h2 = histcounts(data2,edges); h3 = histcounts(data3,edges); h4 = histcounts(data4,edges);
figure bar(edges(1:end-1),[h1; h2; h3; h4]')
![](/matlabcentral/answers/uploaded_files/53754/test.png)
Just as an FYI, I do think there is good reason to avoid the side-by-side histogram, which is that the bins do not line up where the actual data are, so it can be misleading. I speculate that this is why MathWorks eliminated this option.
0 Commenti
Più risposte (1)
Image Analyst
il 7 Giu 2016
It doesn't look like histogram() or histcounts() processes matrices in columns anymore. So you'd have to do the overlapping bars like in the help, rather than the side-by-side bars.
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!