How to avoid multi-column histogram made using histogram() from overlapping the data to get output similar to the one got with hist()

5 views (last 30 days)
Since MATLAB says that "hist is not recommended. Use histogram instead", I want to ask how to get the same result using histogram() like the one we get with hist() when making a multi-column histogram.
e.g;
x = randn(1000,3);
hist(x)
The above code gives this:
If we use histogram(), i.e.
x = randn(1000,3);
histogram(x)
The output is:-
If I use it like this:-
x = randn(1000,3);
histogram(x(:,1));
hold on;
histogram(x(:,2))
histogram(x(:,3))
I get this:-
I want to get the histogram using histogram() like the one made using hist() (as in first figure)

Answers (1)

Benjamin Kraus
Benjamin Kraus on 2 Aug 2016
This isn't a perfect solution, but you can use a combination of histcounts + bar:
x = randn(1000,3);
[N1,edges] = histcounts(x(:,1));
N2 = histcounts(x(:,2),edges);
N3 = histcounts(x(:,3),edges);
ctrs = [edges(1:end-1)+edges(2:end)]/2;
bar(ctrs',[N1;N2;N3]');

Products

Community Treasure Hunt

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

Start Hunting!