Normalize a histogram to a different datasets, Normalize two histograms to their sum

6 ビュー (過去 30 日間)
MJ
MJ 2019 年 1 月 31 日
コメント済み: MJ 2019 年 2 月 1 日
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
Adam Danz 2019 年 1 月 31 日
Instead of normalizing by dividing by 100 & 15, why not just divide by 115?
MJ
MJ 2019 年 1 月 31 日
編集済み: MJ 2019 年 1 月 31 日
I don't divide it manually by anything, i am telling how MATLAB does it, i am -in fact- looking for a way to divide by 115 (a user defined number let's say).

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

採用された回答

Adam Danz
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
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.
MJ
MJ 2019 年 2 月 1 日
thank you very much, i appreciate it

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by