The Wayback Machine - https://web.archive.org/web/20111120195949/http://www.mathworks.com:80/help/techdoc/creating_plots/f6-27628.html
Skip to Main Content Skip to Search
Product Documentation

Plotting with Two Y-Axes

Introduction

The plotyy function enables you to create plots of two data sets and use both left and right side y-axes. You can also apply different plotting functions to each data set. For example, you can combine a line plot with a stem plot of the same data.

t = 0:pi/20:2*pi;
y = exp(sin(t));
plotyy(t,y,t,y,'plot','stem')

Images shows a graph with two y axes

Combining Linear and Logarithmic Axes

You can use plotyy to apply linear and logarithmic scaling to compare two data sets having different ranges of values.

t = 0:900; A = 1000; a = 0.005; b = 0.005;
z1 = A*exp(-a*t);
z2 = sin(b*t);
[haxes,hline1,hline2] = plotyy(t,z1,t,z2,'semilogy','plot');

This example saves the handles of the lines and axes created to adjust and label the graph. First, label the axes whose y value ranges from 10 to 1000. This is the first handle in haxes because it was specified first in the call to plotyy. Use the axes function to make haxes(1) the current axes, which is then the target for the ylabel function.

axes(haxes(1))
ylabel('Semilog Plot')

Now make the second axes current and call ylabel again.

axes(haxes(2))
ylabel('Linear Plot')

You can modify the characteristics of the plotted lines in a similar way. For example, to change the line style of the second line plotted to a dashed line, use the statement

set(hline2,'LineStyle','--')

See Using Multiple X- and Y-Axes for an example that employs double x- and y-axes.

See LineSpec for additional line properties.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A; sessions led by MATLAB experts.

 © 1984-2011- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS