Graphics |
Plotting with Two Y-Axes
The plotyy
command 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')The graph is a plot of the mathematical function e to the sin of t, where t ranges from zero to 2 pi. The left side y-axis is blue and corresponds to a plotted blue line that passes through the circular markers of the stem plot. The stems are green as is their corresponding right side y-axis. Stems are vertical line extending from the x-axis to the plotted data value and terminated in a circular marker.
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
command to make haxes(1)
the current axes, which is then the target for the ylabel
command.
Now make the second axes current and call ylabel
again.
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
See Using Multiple X- and Y-Axes for an example that employs double x- and y-axes.
See LineSpec
for additional line properties.
Plotting Imaginary and Complex Data | Setting Axis Parameters |
© 1994-2005 The MathWorks, Inc.