hisfit with same bin edges

16 次查看(过去 30 天)
Katherine Zheng
Katherine Zheng 2022-5-3
回答: Neelanshu 2023-11-23
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.

回答(1 个)

Neelanshu
Neelanshu 2023-11-23
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!

Translated by