威布尔曲线绘图

时间:2017-11-10 16:01:53

标签: matlab matlab-figure

我想在同一图表上绘制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 个答案:

答案 0 :(得分:0)

有几种方法可以解决您的问题。我想到了两个:

  1. 删除条形图并设置限制。
  2. 如果您不想要条形图,为什么要绘制它?您的代码将其用作输入,但没有理由不使用数据本身的限制。也许你在其他部分用于其他目的而不显示。如果是这种情况,请使用选项 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. 删除图表
    2. 这非常简单,只需要很小的改动。在% --- Plot data originally in dataset "model data"部分中,在命令set(hLine,'FaceColor' ...之后放入另一个命令:

      delete(hLine)
      

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