Normalize a histogram to a different datasets, Normalize two histograms to their sum
23 次查看(过去 30 天)
显示 更早的评论
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,

4 个评论
采纳的回答
Adam Danz
2019-1-31
编辑:Adam Danz
2019-1-31
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 个评论
Adam Danz
2019-2-1
Nice work! I didn't look through the code but if you have any other questions I'd be glad to help.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!