www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@localmod/plot.m

    function h= plot(L,Xd,Yd,DataOk,Options,AxHand,LocCol)
%PLOT plot localmod with data
% 
% plot(L,X,Y,bdflag,Transform,AxHand,AbsX)
%  L         localmod object
%  X         sweepset data
%  Y         sweepset data
%  AxHand    axes to plot in
%  optional flags
%      bdflag    logical to show bad data  (1)
%      Transform logical to plot in ytrans (0)
%      CI        plot confidence intervals (1)
%      AbsX      to use X relative to datum (0)
%      ModelRange use bounds from model to determine plotting range

%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.

if nargin < 4
    % default data indicator
    DataOk= isfinite(double(Yd));
end
if nargin < 5
    Options= [1 0 1 1 1];
end
if nargin < 6
    AxHand= gca;
end
ColOrder = AxHand.ColorOrder;
if nargin<7
    LocCol = ColOrder(1,:);
end

if length(Options)==3
    Options(4:5)= [0 0];
end

BDflag    = Options(1) ;

Transform = Options(2) && HasTransform(L);
CI        = Options(3);
AbsX      = Options(4);
ModelRange= Options(5);



% OK data for plot
X= double(Xd(DataOk,:));
Y= double(Yd(DataOk));


Types= InputFactorTypes(L);
ylabstr= ResponseLabel(L,Transform);

% record of lines already on axes
hOld= findobj(AxHand,'Type','line');

set(ancestor(AxHand,'figure'),'CurrentAxes',AxHand)
PredvObs= length(find(Types==1)) > 1;
if PredvObs 
    % when multiple local factors
    % plot Y vs yhat
    yhat= EvalModel(L,X);
    Xvar=Y;
    lims=[min(min(Y,yhat)) max(max(Y,yhat))];
	 xlims=lims;
    if Transform
        Xvar(:,:)= ytrans(L,Xvar);
		  xlims= ytrans(L,lims);
    end
    
	 
    % draw line
    h= line('Parent',AxHand,...
        'Color',LocCol,...
        'LineStyle','none',...
        'Marker','.','MarkerSize',15,...
        'XData',Xvar,...
        'YData',yhat);
    xregGui.setLegendData(h, 'Data');
    % also plot the line y=x
    hL = line(xlims,lims,'Marker','none','LineStyle','-',...
        'Color','k','Parent',AxHand);
    mbcgui.hgclassesutil.setNotPickable(hL);
    
    xregGui.setLegendData(hL, 'Predicted=Observed');
    xlabstr= ylabstr;
    ylabstr = ['Predicted ', ylabstr];
else
    % plot Y vs X
    [h,Xvar] = i_plotYvX(L,X,Y,AbsX,ModelRange,AxHand,LocCol);
    xlabstr = InputLabels(L);
    if AbsX
        % 		xlabstr= xdat.Symbols{1};
        xlabstr = xlabstr{1};
    else
        % 		xlabstr= [xdat.Symbols{1} ' - DATUM'];
        xlabstr = [xlabstr{1} ' - DATUM'];
    end
	 
end

set(get(AxHand,'XLabel'),...
    'String',xlabstr,...
    'Interpreter','none',...
    'FontSize',8);
set(AxHand,'XGrid','on','YGrid','on');
%set(get(AxHand,'title'),'string',sprintf('Test %2g',testnum(Yd)),'FontWeight','bold');

if Transform
    % this transforms all the new data
    hnew= findobj(AxHand,'Type','line');
    hnew= setdiff(hnew,hOld);
    for i=1:length(hnew);
        yd= get(hnew(i),'YData');
        yd= ytrans(L,yd);
        set(hnew(i),'YData',yd);
    end
end
set(get(AxHand,'YLabel'),...
    'String',ylabstr,...
    'Interpreter','none',...
    'FontSize',8);

if CI
    
    Xs= code(L,X);
    
    if Transform 
        % transformed yhat and sigma(yhat)^2
        p   = evalpev(Xs,L);
        yhat= eval(L,Xs); %#ok<EVLC>
        if isTBS(L)
            yhat= ytrans(L,yhat);
        end
    else
        % untransformed yhat and sigma(yhat)^2
        [p,yhat] = pev(L,Xs,0);
    end
    
    alpha= 0.95;
    
    % build confidence intervals
    df= length(Y)-size(L,1) ;
    ts= tinv(1-(1-alpha)/2,df)*sqrt(p);
    ci_lo= yhat-ts;
    ci_hi= yhat+ts;
    
    if size(Y,1)<100
        % make plot lines with bars at top and bottom
        nanm= NaN(size(Y));
        d= (max(Xvar)-min(Xvar))/150;
        xci= [Xvar,Xvar,nanm,Xvar-d,Xvar+d,nanm,Xvar-d,Xvar+d,nanm]';
        yci= [ci_hi,ci_lo,nanm,ci_lo,ci_lo,nanm,ci_hi,ci_hi,nanm]';
        style= '-';
    else
        % plot ci envelope as dashed lines
        nanm= NaN(size(Y));
        xci= [Xvar,Xvar,nanm]';
        yci= [ci_hi,ci_lo,nanm]';
        style= '-';
    end
    % do plot of ci lines
    tmp=plot(xci(:),yci(:),style,'Parent',AxHand,'Color',LocCol);
    set(tmp,'Tag','localCI');
    mbcgui.hgclassesutil.setNotPickable(tmp);
    
    xregGui.setLegendData(tmp, false);
end


if  BDflag
    % show outlier points
    
    bdX= double(Xd(~DataOk,:));
    bdY= double(Yd(~DataOk));
    if ~isempty(bdY)
        if PredvObs 
            % plot observed on X axis, and predicted on Y axis
            bdXvar = bdY;
            bdY = EvalModel(L,bdX);
            if Transform 
                bdXvar= ytrans(L,bdXvar);
            end
        else
            if AbsX
                bdXvar= bdX;
            else
                bdXvar= code(L,bdX);
            end
        end
        if Transform 
            bdY= ytrans(L,bdY);
        end
        
        if ~isreal(bdY)
            % handle imaginary transforms
            bdY(abs(imag(bdY))>sqrt(eps))= NaN;
            bdY= real(bdY);
        end
        
        hL = plot(bdXvar(:,1), bdY, 'x',...
            'Parent',AxHand,...
            'MarkerEdgeColor',get(h,'Color'),...
            'LineWidth',1.5,...
            'MarkerSize',8,...
            'Tag','BDPts');
        mbcgui.hgclassesutil.setNotPickable(hL);
        xregGui.setLegendData(hL, 'Removed data');
    end
end

annotate(L,X,Y,AxHand);


%--------------------------------------------------------------------------
function [h,negdelta] = i_plotYvX(L,X,Y,AbsX,ModelRange,AxHand,LocCol)

Datum= datum(L);
if ~(L.DatumType && isfinite(Datum))
    negdelta= X(:,1);
    Datum=0;
elseif AbsX
    % Setup -MBT array for plotting purposes.
    negdelta= X(:,1);
else
    negdelta= X(:,1)-Datum;
end
% Setup arrays for plotting modelled response.

HasRFVals= ~isempty(strfind( [L.Type.Function],'.Value'));
if HasRFVals
    % plot response feature values 
    fVals= unique(L.Values+Datum);
elseif L.DatumType
    fVals= Datum;
else
    fVals=[];
end

% find some ranges
LB= min([fVals;double(X(:,1))]);
UB= max([fVals;double(X(:,1))]);
if ModelRange
    bnds= getcode(L);
    if ~((bnds(1)==-1 || bnds(1)==0)  && bnds(1,2)==1) 
        LB= bnds(1,1);
        UB= bnds(1,2);
    end
end
% x data for plotting
x= linspace(LB,UB,101)';

if (AbsX) 
    xd=x;
    d= Datum;
else
    xd = x-Datum;
    d=0;
end

% Predicted values
if nfactors(L)==1
    y= EvalModel(L,x);
else
    xd=X(:,1);
    y= EvalModel(L,X);
end
% draw local fit line (color = LocCol)
hL = line('Parent',AxHand,...
    'Tag','localfit',...
    'Color',LocCol,...
    'XData',xd,...
    'YData',y);
xregGui.setLegendData(hL, 'Model fit');
mbcgui.hgclassesutil.setNotPickable(hL);

% draw blue local data points
h= line('Parent',AxHand,...
    'Color',LocCol,...
    'Marker','.',...
    'MarkerSize',15,...
    'LineStyle','none',...
    'XData',negdelta,...
    'YData',Y);
xregGui.setLegendData(h, 'Data');

if HasRFVals
    % response features at function values
    hL = line('XData',fVals-Datum+d,'YData',EvalModel(L,fVals),...
        'Marker','+',...
        'Tag','localfcnvals',...
        'Color','r',...
        'LineWidth',1.5,...
        'LineStyle','none',...
        'Parent',AxHand);
    xregGui.setLegendData(hL, 'Response features');
end
if L.DatumType
    % display datum point
    hL = line('XData',d,'YData',EvalModel(L,Datum),...
        'LineWidth',1.5,...
        'Tag','localdatum',...
        'Marker','*','Color','r',...
        'LineStyle','none',...
        'Parent',AxHand);
    xregGui.setLegendData(hL, 'Datum');
end