in the histogram function using "Normalization", "cdf" to draw out the histogram and cdf does not match, the end of histogram doesn't reach "1"

Here is my code:
data = readtable("owid-covid-data_Subset3.xlsx", opts);
raw_data = data(:,{'location', 'date', 'icu_patients'});
% data scale
march_2020 = isbetween(raw_data.date, datetime(2020,3,1), datetime(2020,3,31));
march_2021 = isbetween(raw_data.date, datetime(2021,3,1), datetime(2021,3,31));
uk = raw_data(march_2021 & ismember(raw_data.location,{'United Kingdom'}), :);
cdfplot(uk.icu_patients);
hold on;
nbins = 20;
h = histogram(uk.icu_patients,nbins,'Normalization','cdf');
xlabel('icu patients scale');
ylabel('frequency');
title('cumulative frequency plot using 20 bins in United Kingdom');
Which step did I make a mistake?

3 Comments

I think we need to see the data to diagnose this problem, so we can replicate exactly what you are seeing. (You can do that using the paperclip icon in the INSERT section of the toolbar.)
The reason I say this is that I get the expected result if I just put in some random data:
data = randn(1000,1);
cdfplot(data);
hold on;
nbins = 20;
h = histogram(data,nbins,'Normalization','cdf');
xlabel('icu patients scale');
ylabel('frequency');
title('cumulative frequency plot using 20 bins in United Kingdom');
Thank you for your advice! I figured it out, It was my choice of data that was the problem, the dataset 'UK' contained some missing data and there was no data between March 2020 so after I changed the size of the data it worked. : )

Sign in to comment.

 Accepted Answer

Are there any NaNs (or something similar) in uk.icu_patients?
It seems that histogram and cdfplot treat NaNs differently
x = rand(1000,1);
x(1:2:end) = nan;
figure
cdfplot(x)
hold on
histogram(x,'Normalization','cdf')

More Answers (0)

Products

Release

R2022a

Asked:

on 9 Oct 2022

Commented:

on 9 Oct 2022

Community Treasure Hunt

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

Start Hunting!