Search code examples
matlabmatlab-figureweibull

Weibull Curves plotting


I want to plot 2 weibull curves( sample and model) on the same graph.

I have worked in my code. Attached is the results i am having, i do not want the bar chart present, only my 2 weibull curves, but I am not able to do it.

Can someone please help me???

ps: In this case, @Guto has helped me edit my codes and now they work wonderfully.

Thanks

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);

Solution

  • There are a few options to solve your problem. There are the two that came to my mind:

    1. Remove the bar plots and make the limit.

    If you don't want the bar plots why are you plotting it? Your code use it as input, but there is no reason to not use the limits from the data itself. Maybe you use for other purposes in another part not show. If this is the case, use the option 2.

    For this, you just need fitdist and pdf functions. However, you need to check the min and max of both data sets and uses whatever is smaller or bigger. Here is just the necessary code to plot both lines:

    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. Delete the graph

    That is quite straightforward and require only a small change. In the part % --- Plot data originally in dataset "model data", after the command set(hLine,'FaceColor' ... put the another command:

    delete(hLine)
    

    It will remove the latest graph assigned to hLine, that are the bars. This will give the same output (except by small variations on limits) as above.