hisfit with same bin edges

9 views (last 30 days)
Katherine Zheng
Katherine Zheng on 3 May 2022
Answered: nick on 23 Nov 2023
Hi, I have 9 histogram plot and I want to plot their distribution for comparison. The 9 data sets have different ranges (min and max values). Some of the data sets are discontinue when approaching the maximum value and the number of data in each sets are not the same. I have plot them with the same bin edges by specifying the bin edges according to the max and min values of the whole 9 data sets (the blue shaded bars). This would results in some bins having no data. My issue is with the hisfit (Gamma) that I can only specify bin counts not the bin edges and it would plot with equally spaced bin width which is not what I have in the histogram.
Data attached (each row represents one data sets).
Please kindly help.

Answers (1)

nick
nick on 23 Nov 2023
Hi Katherine,
I understand that you are facing an issue related to using bin edges instead of bin counts while using the function 'histfit'.
I am assuming that ‘hisfit’ mentioned in the legend refers to the data in ‘KT.mat’. You can try the following work around to specify bin edges. It involves fitting the data using Curve Fitting Toolbox as shown below:
SIG = 1.95;
MU = 5.5;
X = randn(100,1)*SIG + MU; % Replace it with the data in KT.mat
% Define the bin edges you want
EDGES = [1:0.5:10];
% Bin the data according to the predefined edges:
Y = histcounts(X, EDGES);
% Fit a normal distribution using the curve fitting tool:
binCenters = conv(EDGES, [0.5, 0.5], 'valid'); % moving average
[xData, yData] = prepareCurveData( binCenters, Y );
ft = fittype( 'gauss1' );
fitresult = fit( xData, yData, ft );
disp(fitresult); % optional
% Plot fit with data (optional)
figure();
histogram(X, EDGES); hold on; grid on;
plot(fitresult);
You may refer to the attached documentation to learn more about Curve Fitting Toolbox:
Hope this helps,
Regards,
Neelanshu

Community Treasure Hunt

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

Start Hunting!