我想在同一张图上绘制2条weibull曲线(样本和模型)。
我一直在我的代码中工作。附加的结果是我有,我不希望条形图存在,只有我的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);发布于 2017-11-10 16:25:44
有几个选项可以解决你的问题。我想到了两个:
如果你不想要酒吧的情节,你为什么要画它?您的代码使用它作为输入,但是没有理由不使用数据本身的限制。也许你在另一个部分中是为了其他目的而使用的,而不是展示。如果是这样的话,请使用选项2。
为此,您只需要fitdist和pdf函数。但是,您需要检查两个数据集的min和max,并使用任何更小或更大的数据集。下面是绘制这两行的必要代码:
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')这是相当简单的,只需要一个小的改变。在% --- Plot data originally in dataset "model data"部分中,在命令set(hLine,'FaceColor' ...之后放置另一个命令:
delete(hLine)它将删除分配给hLine的最新图表,即条形图。这将提供与上述相同的输出(限制上的微小变化除外)。
https://stackoverflow.com/questions/47226762
复制相似问题