Create chart with two y-axes
the side of the current axes associated with the left y-axis.
Subsequent graphics commands target the left side. If the current
axes do not include two y-axes, then this command
adds a second y-axis. If there are no axes, then
this command first creates them.
yyaxis activates the side of the current axes
associated with the right y-axis. Subsequent graphics
commands target the right side.
Create axes with a y-axis on both the left and right sides. Plot a set of data against the left y-axis. Then, use
yyaxis right to activate the right side so that subsequent graphics functions target it. Plot a second set of data against the right y-axis and set the limits for the right y-axis.
x = linspace(0,10); y = sin(3*x); yyaxis left plot(x,y) z = sin(3*x).*exp(0.5*x); yyaxis right plot(x,z) ylim([-150 150])
Create a chart with two y-axes and add a title and axis labels to each side.
Load the matrix
hwydata from the example file
accidents.mat. Create a scatter plot of the fifth column in
hwydata against the left y-axis. Add a title and axis labels.
load('accidents.mat','hwydata') ind = 1:51; drivers = hwydata(:,5); yyaxis left scatter(ind,drivers) title('Highway Data') xlabel('States') ylabel('Licensed Drivers (thousands)')
Create a second scatter plot of the seventh column in
hwydata against the right y-axis. Then, label the right y-axis.
pop = hwydata(:,7); yyaxis right scatter(ind,pop) ylabel('Vehicle Miles Traveled (millions)')
Plot two lines against the left y-axis by using the
hold on command.
x = linspace(0,10); yl1 = sin(x); yl2 = sin(x/2); yyaxis left plot(x,yl1) hold on plot(x,yl2)
Plot two lines against the right y-axis. The
hold command affects both the left and right y-axes, so you do not need to reissue it. After plotting, turn
hold back off.
yr1 = x; yr2 = x.^2; yyaxis right plot(x,yr1) plot(x,yr2) hold off
Clear the left side by making it active and then using the
yyaxis left cla
Create a figure with two subplots and assign the
Axes objects to variables
ax2. Add a second y-axis to the upper subplot by specifying
ax1 as the first input to
yyaxis. If you do not specify the axes, then
yyaxis adds a second y-axis to the current axes.
x = linspace(1,10); ax1 = subplot(2,1,1); yyaxis(ax1,'left') plot(ax1,x,sin(x)) yyaxis(ax1,'right') plot(ax1,x,exp(x)) ax2 = subplot(2,1,2); plot(ax2,1:10)
ax— Target axes
Target axes, specified as an
Axes object. If
you do not specify an
Axes object, then
the current axes.
When working with two y-axes, you cannot:
Rotate the axes (2-D view only).
Copy the axes object using
To determine which side of the axes is active, query
Axes object. The property is set to
the left side is active and
'right' when the right
side is active. The
YAxisLocation property for
Axes object with two y-axes
is read only.
To clear the active side, use
To clear both sides of the axes and remove the right y-axis,
cla reset. Alternatively, you can wait to clear
both sides and remove the right y-axis until
the next plotting command by setting the
Axes object to
Grid lines correspond with the tick mark locations along the left y-axis.
Plots associated with a particular side of the axes use the
same color as the y-axis on that side. If a side
contains multiple lines, then the lines cycle through the line style
order. The left y-axis uses the first color in
the color order of the
Axes object, and the right y-axis
uses the second color.
If you add a second y-axis to an
that contains charts, then the existing charts and the left y-axis
do not change colors. The right y-axis uses the
next color in the color order.
Axes properties related to the y-axis have
two values. However, MATLAB® gives access only the value for the
active side. For example, if the left side is active, then the
of the axes object contains the limits for the left y-axis.
Similarly, if the right side is active, then the
contains the limits for the right y-axis.
An exception is that the
YAxis property of the
Axes object contains an array of two ruler
objects (one for each y-axis). You can use the rulers to access
each y-axis without depending on the active side. For an
example, see Modify Properties of Charts with Two y-Axes.