Normalize a histogram to a different datasets, Normalize two histograms to their sum
26 views (last 30 days)
Show older comments
Hi everyone,
i want to plot two datasets on the same histogram, however one group of the dataset represent cars going fast and the other group represent the slow ones, i want to plot both of them on the same histogram, but when i normalize it, it uses the number of observations for each group (e.g. if i have 100 fast cars and 15 slow cars it will be normalized (divided) according to 100 & 15), but i want both groups to be normalized to their sum = 115 vehicles so that the slow cars will appear in a tiny bars next to the fast ones. you can see the figure to make it more clear.
can anyone help me with that please?
thanks,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/202242/image.jpeg)
4 Comments
Adam Danz
on 31 Jan 2019
Instead of normalizing by dividing by 100 & 15, why not just divide by 115?
Accepted Answer
Adam Danz
on 31 Jan 2019
Edited: Adam Danz
on 31 Jan 2019
Idea 1
This is a little bit of a hack but you could add NaN values to each histogram input so that it has 115 elements. Then the probability normalization will normalize by n=115 rather than the number of non-nan datapoints. That would look something like this.
data = nan(1,115);
data(1:length(NBL2fsummary(:,4))) = NBL2fsummary(:,4);
NBL2fhistogram = histogram (data,'BinWidth',10,'Normalization','probability');
Idea 2
Use histcounts() and bar() instead of histogram() and normalize the data yourself. Pro: you're doing the normalization instead of using a black box. Con: you lose a lot of nice features that come with histogram().
It would look something like this:
n = histcounts(NBL2fsummary(:,4), edges); %you create the edges
m = histcounts(NBL2ssummary(:,4), edges);
count = sum([n,m]); %number of data points (used in normalization)
b2 = bar(edges(1:end-1), n/count, 'histc'); % n/count is the normalization
3 Comments
Adam Danz
on 1 Feb 2019
Nice work! I didn't look through the code but if you have any other questions I'd be glad to help.
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!