Documentation Center

  • Trial Software
  • Product Updates

Create Graph with Two y-Axes

This example shows how to create a graph with two y-axes, label the axes, and display the grid lines.

Define and Plot Data

Define constants A, a, and b. Define data t, z1, and z2.

A = 1000;
a = 0.005;
b = 0.005;

t = 0:900;
z1 = A*exp(-a*t);
z2 = sin(b*t);

Use plotyy to create a graph with two y-axes. Plot z1 versus t using semilogarithmic scaling. Plot z2 versus t using linear scaling. Return the handles to the two axes in array haxes, and return the two line handles in hline1 and hline2.

figure
[haxes,hline1,hline2] = plotyy(t,z1,t,z2,'semilogy','plot');

The left y-axis corresponds to the first set of data plotted, which is the semilogarithmic plot for z1. The first axes handle, haxes(1), and the line handle, hline1, correspond to the first set of data.

The right y-axis corresponds to the second set of data plotted, which is the line plot for z2. The second axes handle, haxes(2), and the line handle, hline2, correspond to the second set of data.

Label the Axes

Label the left y-axis by passing the first axes handle to the ylabel function. Then, label the right y-axis by passing the second axes handle to the ylabel function. Label the x-axis using either axes handle.

ylabel(haxes(1),'Semilog Plot') % label left y-axis
ylabel(haxes(2),'Linear Plot') % label right y-axis
xlabel(haxes(2),'Time') % label x-axis

Modify Line Appearance

Use the line handles, hline1 and hline2, to change the appearance of the lines.

set(hline1,'LineStyle','--','LineWidth',2);
set(hline2,'LineWidth',2);

Display Grid Lines

Display the log grid associated with the left y-axis by passing the first axes handle to the grid function.

grid(haxes(1),'on');

To display the linear grid associated with the right y-axis instead, use grid(haxes(2),'on').

See Also

| |

Related Examples

Was this topic helpful?