Main Content

plot

Plot cfit or sfit object

Description

Surfaces

plot(sfit) plots the surface given in the sfit object sfit over the range of the current axes (gca). If no current axes exist, the function plots the surface over the range of the data used to create sfit.

example

plot(sfit,[x,y],z) plots sfit together with a scatter plot of the data in x, y, and z. The axes limits are given by the ranges of x and y.

plot(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in the previous syntaxes for surfaces. For example, you can specify the type and limits of the plot.

Curves

plot(cfit) plots the curve given in the cfit object cfit over the range of the current axes (gca). If no current axes exist, the function plots the curve over the range of the data used to create cfit.

plot(cfit,x,y) plots cfit together with a scatter plot of the data in x and y. The axes limits are given by the range of x.

example

plot(cfit,x,y,DataLineSpec) specifies the color, marker symbol, and line style used to plot the scatter plot data.

plot(cfit,FitLineSpec,x,y,DataLineSpec) specifies the color, marker symbol, and line style that plot uses to plot the curve given in cfit.

plot(cfit,x,y,outliers) specifies the scatter plot data to treat as outliers, and plots them in a different color.

plot(cfit,x,y,outliers,OutlierLineSpec) specifies the color, marker symbol, and line style used to plot the outliers.

plot(cfit,FitLineSpec,x,y,DataLineSpec,outliers,OutlierLineSpec) specifies the color, marker symbol, and line style used to plot the curve, scatter plot data, and outliers.

example

plot(___,ptype) specifies the plot type using any of the input arguments combinations in the previous syntaxes for curves.

example

plot(___,ptype,level) specifies the confidence level for the prediction bounds when ptype is "predfunc" or "predobs".

Surfaces and Curves

plot(ax,___) plots into the axes specified by ax instead of gca. The argument ax can precede any of the input argument combinations in the previous syntaxes.

H = plot(___) returns a vector of handles to the plotted objects.

Examples

collapse all

Load the franke data set.

load franke

The vectors x, y, and z contain data generated from Franke's bivariate test function.

Fit a Lowess smoothing model to the data in x, y, and z.

ls = fit([x,y],z,"lowess");

ls is an sfit object that contains the results of fitting the Lowess smoothing model to the data.

Plot ls and change the view of the figure.

plot(ls)
view([19.5 40.0])

Figure contains an axes object. The axes object contains an object of type surface.

The plot shows that the Lowess smoothing fit is a wavy surface that lies between z=0 and z=1.5 across the values for x and y.

Load the titanium data set. Convert the data from row to column vectors.

[x, y] = titanium;
x = x';
y = y';

x contains temperature measurements and y contains measurements for a property of titanium.

Fit a Gaussian model to the x and y data.

g = fit(x,y,"gauss2");

g is a cfit object that contains the results of fitting the Gaussian model to the data.

Plot g with a scatter plot of the data.

plot(g,x,y)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Fitted curve.

The plot shows that g closely follows the majority of the data, and the y data spikes when x is approximately 900.

Generate some data for a baseline sinusoidal signal using the linspace and sin functions.

xdata = linspace(0,2*pi,60)'; 
y0 = sin(xdata);

xdata is a vector of 60 points between 0 and 2π, and y0 is a vector of values given by evaluating the sine function at the values in xdata.

Generate noise from a Gaussian distribution using the randn function.

rng(0,"twister") %  For reproducibility
gnoise = y0.*randn(size(y0));

To generate impulse noise, first use the randperm and round functions to create a vector of random indices.

leny0 = length(y0);
p = randperm(leny0);
stop = round(leny0/5);
idx = p(1:stop);

idx is a vector of integers representing the indices of y0.

Create a vector of impulse noise using the zeros function. Then, use the sign function to assign the values –5 and 5 to the noise vector at positions in idx where the elements of y0 are negative and positive, respectively.

szy = size(y0);
spnoise = zeros(szy);
yidx = y0(idx);
spnoise(idx) = 5*sign(yidx);

Add the noise vectors to y0.

ydata = y0 + gnoise + spnoise;

ydata is a vector of noisy data with nonconstant variance.

Fit a sinusoidal model to ydata.

f = fittype("a*sin(b*x)"); 
fit1 = fit(xdata,ydata,f,StartPoint=[1 1]);

fit1 contains the results of fitting a sinusoidal model using least-squares fitting.

Create a vector of outliers from the points in xdata and ydata that are more than 1.5 standard deviations away from the model in fit1.

fdata = feval(fit1,xdata);
outliers = abs(fdata - ydata) > 1.5*std(ydata);

Refit the data with the outliers excluded.

fit2 = fit(xdata,ydata,f,StartPoint=[1 1],...
           Exclude=outliers);

fit2 contains the results of fitting a sinusoidal model to the data with the outliers excluded.

Fit a third model using a robust fitting algorithm.

fit3 = fit(xdata,ydata,f,StartPoint=[1 1],Robust="on");

fit3 contains the results of fitting a sinusoidal model to the data using the bisquare-weights fitting algorithm.

To compare the fitted models, plot the data, outliers, and results of the fits.

plot(fit1,xdata,ydata,outliers) 
hold on
plot(fit2)
plot(fit3)
xlim([0 2*pi])
legend("data","outlier","fit1","fit2","fit3")

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 5 objects of type line. One or more of the lines displays its values using only markers These objects represent data, outlier, fit1, fit2, fit3.

The plot shows each fit with a different line color. All three fits follow the bulk of the data. fit1 comes closer to the outliers than the other two fits.

Plot the residuals for fit1.

figure 
plot(fit1,xdata,ydata,"residuals") 
hold on
xlabel("xdata")
ylabel("residuals")
hold off

Figure contains an axes object. The axes object with xlabel xdata, ylabel residuals contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Zero line.

The plot shows that the residuals for fit1 have nonconstant variance across the values in xdata.

Load the census data set and fit a polynomial using the variables cdate and pop. These variables contain data for the year the census was taken, and the population size, respectively. Consider all dates before 1850 to be outliers.

load census
fitresult = fit(cdate,pop,"poly2",Exclude=cdate<1850)
fitresult = 
     Linear model Poly2:
     fitresult(x) = p1*x^2 + p2*x + p3
     Coefficients (with 95% confidence bounds):
       p1 =    0.006709  (0.005497, 0.00792)
       p2 =      -24.15  (-28.81, -19.5)
       p3 =   2.175e+04  (1.728e+04, 2.621e+04)

Plot the cfit object fitresult with the default line styles.

plot(fitresult,cdate,pop,cdate<1850)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Excluded data, Fitted curve.

To modify the line styles, you can specify one or more of these arguments for the plot: DataLineSpec, FitLineSpec, and OutlierLineSpec. For this example, set the data line style as blue points, the fit line style as a solid red line, and the outlier line style as green plus signs.

plot(fitresult,"r-",cdate,pop,"b.",cdate<1850,"g+")

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Excluded data, Fitted curve.

Input Arguments

collapse all

Surface fit object to plot, specified as an sfit object.

Curve fit object to plot, specified as a cfit object.

x-coordinates for the scatter plot data, specified as a numeric column vector. When plotting cfit objects, you must also specify y when you specify x. When plotting sfit objects, you must also specify y and z when you specify x. x and y must be the same size.

Data Types: single | double

y-coordinates for the scatter plot data, specified as a numeric column vector. When plotting cfit objects, you must also specify x when you specify y. When plotting sfit objects, you must also specify x and z when you specify y. x and y must be the same size.

Data Types: single | double

z-coordinates for the scatter plot data, specified as a numeric column vector. You can specify z only when plotting sfit objects, and you must also specify x and y. z and x must be the same size.

Data Types: single | double

Indicator to treat scatter plot data as outliers, specified as a logical expression, index vector, or a logical vector. You can specify outliers only when plotting cfit objects.

  • Logical expression — plot treats data points that satisfy the expression as outliers.

  • Index vector — plot treats data points at the specified indices as outliers.

  • Logical vector — plot treats data points at positions where outliers is true as outliers. You can create a logical vector to specify outliers by using the excludedata function.

For an example, see Exclude Points from Fit.

Example: x > 10 treats data with values for x greater than 10 as outliers.

Example: [1 10 25] treats data at indices 1, 10, and 25 as outliers.

Example: [zeros(100,1);ones(50,1)] treats the last 50 data points as outliers.

Data Types: logical | single | double

Line style, marker, and color for the scatter plot data, specified as a character vector or string scalar containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

You can specify DataLineSpec only when plotting cfit objects.

Line StyleDescription
-Solid line (default)
--Dashed line
:Dotted line
-.Dash-dot line
MarkerDescription
oCircle
+Plus sign
*Asterisk
.Point
xCross
sSquare
dDiamond
^Upward-pointing triangle
vDownward-pointing triangle
>Right-pointing triangle
<Left-pointing triangle
pPentagram
hHexagram
ColorDescription

y

Yellow

m

Magenta

c

Cyan

r

Red

g

Green

b

Blue

w

White

k

Black

Example: "--or" specifies a red dashed line with circle markers.

Data Types: char | string

Line style, marker, and color for the curve given in cfit, specified as a character vector or string scalar containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Line StyleDescription
-Solid line (default)
--Dashed line
:Dotted line
-.Dash-dot line
MarkerDescription
oCircle
+Plus sign
*Asterisk
.Point
xCross
sSquare
dDiamond
^Upward-pointing triangle
vDownward-pointing triangle
>Right-pointing triangle
<Left-pointing triangle
pPentagram
hHexagram
ColorDescription

y

Yellow

m

Magenta

c

Cyan

r

Red

g

Green

b

Blue

w

White

k

Black

Example: "--or" specifies a red dashed line with circle markers.

Data Types: char | string

Line style, marker, and color used to plot the outliers, specified as a character vector or string scalar containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

You can specify OutlierLineSpec only when plotting cfit objects.

Line StyleDescription
-Solid line (default)
--Dashed line
:Dotted line
-.Dash-dot line
MarkerDescription
oCircle
+Plus sign
*Asterisk
.Point
xCross
sSquare
dDiamond
^Upward-pointing triangle
vDownward-pointing triangle
>Right-pointing triangle
<Left-pointing triangle
pPentagram
hHexagram
ColorDescription

y

Yellow

m

Magenta

c

Cyan

r

Red

g

Green

b

Blue

w

White

k

Black

Example: "--or" specifies a red dashed line with circle markers.

Data Types: char | string

Plot type for a curve fit, specified as one of the following:

  • "fit" — Data and fit

  • "predfunc" — Data and fit with prediction bounds for the fit

  • "predobs" — Data and fit with prediction bounds for new observations

  • "residuals" — Residuals

  • "stresiduals" — Standardized residuals (residuals divided by their standard deviation)

  • "deriv1" — First derivative of the fit

  • "deriv2" — Second derivative of the fit

  • "integral" — Integral of the fit

Data Types: char | string

Confidence level for the prediction intervals of a curve fit when ptype is "predfunc" or "predobs", specified as a scalar value in the range (0,1).

Example: 0.99

Data Types: single | double

Target axes, specified as an Axes object. If you do not specify the axes, then plot uses the current axes (gca).

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: plot(sfit,[x,y],z,YLim=[0,1],Exclude=[1 10 25]) specifies the y-axis limits and points to treat as outliers.

Indicator to treat scatter plot data as outliers, specified as a logical expression, index vector, or logical vector.

  • Logical expression — plot treats data points that satisfy the expression as outliers.

  • Index vector — plot treats data points at the specified indices as outliers.

  • Logical vector — plot treats data points at positions where outliers is true as outliers. You can create a logical vector to specify outliers by using the excludedata function.

You must also specify x, y, and z input arguments when you specify Exclude.

For an example, see Exclude Points from Fit.

Example: Exclude=x>10 treats data with values for x greater than 10 as outliers.

Example: Exclude=[1 10 25] treats data at indices 1, 10, and 25 as outliers.

Example: Exclude=[zeros(100,1);ones(50,1)] treats the last 50 data points as outliers.

Data Types: logical | single | double

Surface fit plot style, specified as one of the following:

  • "Surface" — Plot the sfit object as a surface (default).

  • "PredFunc" — Plot the sfit object as a surface with prediction bounds for the function.

  • "PredObs" — Plot the sfit object as a surface with prediction bounds for new observations.

  • "Residuals" — Plot the residuals for the sfit object. When you specify Style as "Residuals", you must also specify x, y, and z.

  • "Contour" — Plot the sfit object as a contour plot.

Example: Style="Contour"

Data Types: char | string

Confidence level for the prediction intervals of a surface fit when Style is "PredFunc" or "PredObs", specified as a scalar value in the range (0,1).

Example: Level=0.99

Data Types: single | double

x-axis limit for a surface plot, specified as a numeric vector with two elements. If you provide x, y, and z data, the axes limits are given by x and y. Otherwise, plot uses the axes limits in sfit.

Example: XLim=[0 1]

Data Types: single | double

y-axis limit for a surface plot, specified as a numeric vector with two elements. If you provide x, y, and z data, the axes limits are given by x and y. Otherwise, plot uses the axes limits in sfit.

Example: YLim=[-100 100]

Data Types: single | double

Handle of the axes for a surface plot, specified as an Axes object.

Example: Parent=ax

Version History

Introduced before R2006a

expand all