Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# plotyy

2-D line plots with y-axes on both left and right side

## Syntax

plotyy(X1,Y1,X2,Y2)
plotyy(X1,Y1,X2,Y2,function)
plotyy(X1,Y1,X2,Y2,'function1','function2')
[AX,H1,H2] = plotyy(...)

## Description

plotyy(X1,Y1,X2,Y2) plots X1 versus Y1 with y-axis labeling on the left and plots X2 versus Y2 with y-axis labeling on the right.

plotyy(X1,Y1,X2,Y2,function) uses the specified plotting function to produce the graph.

function can be either a function handle or a string specifying plot, semilogx, semilogy, loglog, stem, or any MATLAB® function that accepts the syntax

`h = function(x,y)`

For example,

```plotyy(x1,y1,x2,y2,@loglog) % function handle
plotyy(x1,y1,x2,y2,'loglog') % string```

Function handles enable you to access user-defined local functions and can provide other advantages. See @ for more information on using function handles.

plotyy(X1,Y1,X2,Y2,'function1','function2') uses function1(X1,Y1) to plot the data for the left axis and function2(X2,Y2) to plot the data for the right axis.

[AX,H1,H2] = plotyy(...) returns the handles of the two axes created in AX and the handles of the graphics objects from each plot in H1 and H2. AX(1) is the left axes and AX(2) is the right axes.

## Examples

expand all

### Plot Two Data Sets with Different y-Axes

Plot two data sets on one graph using two y-axes.

```x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

figure % new figure
plotyy(x,y1,x,y2);
```

### Add Title and Axis Labels

Plot two data sets using a graph with two y-axes. Add a title and axis labels.

```x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

figure % new figure
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);

title('Multiple Decay Rates')
xlabel('Time (\musec)')

ylabel(hAx(1),'Slow Decay') % left y-axis
ylabel(hAx(2),'Fast Decay') % right y-axis
```

### Change Line Styles

Plot two data sets using a graph with two y-axes. Change the line styles.

```x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

figure % new figure
[hAx,hLine1,hLine2] = plotyy(x,y1,x,y2);

set(hLine1,'LineStyle','--')
set(hLine2,'LineStyle',':')
```

### Combine Different Types of Plots

Plot two data sets using a graph with two y-axes. Use a line plot for the data associated with the left y-axes. Use a stem plot for the data associated with the right y-axes.

```x = 0:0.1:10;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

figure
plotyy(x,y1,x,y2,'plot','stem');
```

### Use Right y-Axis for Two Data Sets

Plot three data sets using a graph with two y-axes. Plot one set of data associated with the left y-axis. Plot two sets of data associated with the right y-axis by using two-column matrices.

```x = linspace(0,10);
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
y3 = 0.2*exp(-0.5*x).*sin(10*x);

figure
[hAx,hLine1,hLine2] = plotyy(x,y1,[x',x'],[y2',y3']);
```