威布尔曲线绘制

Weibull Curves plotting

我想在同一张图上绘制 2 条威布尔曲线(样本和模型)。

我已经在我的代码中工作了。 附件是我得到的结果,我不想要条形图,只有我的 2 条威布尔曲线,但我做不到。

有人可以帮帮我吗???

ps:在这种情况下,@Guto 帮助我编辑了我的代码,现在它们运行得非常好。

谢谢

sample=[4.6
3.6
2.3
2.3
3
3.1
];
model=[8.01
6.75
6.57
6.07
5.94
5.58 
];
% --- Plot data originally in dataset "sample data"
[CdfF,CdfX] = ecdf(sample,'Function','cdf');  % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(sample,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
    'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'sample data';

% Create grid where function will be computed
XLim = get(gca,'XLim');
XLim = XLim + [-1 1] * 0.01 * diff(XLim);
XGrid = linspace(XLim(1),XLim(2),100);




% --- Create fit "Weibull"

% Fit this distribution to get parameter values
pd3 = fitdist(sample, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
dist = makedist('Weibull','a',pd3.ParameterValues(1),'b',pd3.ParameterValues(2))
[h_ad_weibull, p_ad_weibull, adstat_weibull,cv_ad_weibull]= adtest(sample,'Distribution','weibull')

[h_ks_weibull, p_ks_weibull, ksstat_weibull,cv_ks_weibull]= kstest(sample,'CDF',dist)
LegHandles(end+1) = hLine;
LegText{end+1} = 'Weibull';

box on;
figure(1);
hold on;


% --- Plot data originally in dataset "model data"
[CdfF,CdfX] = ecdf(model,'Function','cdf');  % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(model,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
    'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'model data';

% Create grid where function will be computed
XLim = get(gca,'XLim');
XLim = XLim + [-1 1] * 0.01 * diff(XLim);
XGrid = linspace(XLim(1),XLim(2),100);




% --- Create fit "Weibull"

% Fit this distribution to get parameter values
pd3 = fitdist(model, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
dist = makedist('model','a',pd3.ParameterValues(1),'b',pd3.ParameterValues(2))
[h_ad_weibull, p_ad_weibull, adstat_weibull,cv_ad_weibull]= adtest(model,'Distribution','weibull')

[h_ks_weibull, p_ks_weibull, ksstat_weibull,cv_ks_weibull]= kstest(model,'CDF',dist)
LegHandles(end+1) = hLine;
LegText{end+1} = 'Weibull';

box on;
figure(2);

有几个选项可以解决您的问题。我想到了两个:

  1. 移除条形图并进行限制。

如果你不想要条形图,你为什么要绘制它?您的代码将其用作输入,但没有理由不使用数据本身的限制。也许您用于其他目的的另一部分未显示。如果是这种情况,请使用选项 2.

为此,您只需要 fitdistpdf 函数。但是,您需要检查两个数据集的 minmax 并使用更小或更大的值。这里只是绘制两条线的必要代码:

box on; %create a figure and hold it
hold on;
%CREATE A GRID FROM DATA
XGrid=linspace(min(min([sample model]))-0.4*min(min([sample model])),...
    max(max([sample model]))+0.2*max(max([sample model])),100);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(sample, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
% --- Create fit "Weibull"
% Fit this distribution to get parameter values
pd3 = fitdist(model, 'weibull');
YPlot = pdf(pd3,XGrid);
hLine = plot(XGrid,YPlot,'Color',[0.666667 0.333333 0],...
    'LineStyle','-', 'LineWidth',2,...
    'Marker','none', 'MarkerSize',6);
xlabel('Data');
ylabel('Density')
  1. 删除图表

这很简单,只需要一点点改动。在 % --- Plot data originally in dataset "model data" 部分中,在命令 set(hLine,'FaceColor' ... 之后放置另一个命令:

delete(hLine)

它将删除分配给 hLine 的最新图表,即条形图。 这将给出与上面相同的输出(除了限制的小变化)。