www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/CustomizeGraphWithTwoYAxesExample.m

    %% Set Properties for Chart with Two y-Axes
% The |yyaxis| command creates an axes object with _y_-axes on the left and
% right sides. Axes properties related to the _y_-axis have two values.
% However, MATLAB gives access only to the value for the active side. For
% example, if the left side is active, then the |YDir| property of the axes
% object contains the direction for the left _y_-axis. Similarly, if the
% right side is active, then the |YDir| property contains the direction for
% the right _y_-axis. An exception is that the |YAxis| property of the axes
% object contains an array of two numeric ruler objects (one for each
% _y_-axis).
%
% To change the appearance and behavior of a particular _y_-axis, 
% either set the active side, and then change property values for the axes
% object, or use the numeric ruler objects.

%% Change Axes Properties
% Create a chart with two _y_-axes and plot data. Reverse the direction for
% each _y_-axis. Use |yyaxis left| to activate the left side. Next, set
% the direction for the left _y_-axis. Similarly, use |yyaxis right| to
% activate the right side. Then, set the direction for the right
% _y_-axis.

x = [1 2 3];
y1 = [2 6 4; 3 5 4; 5 7 8];
y2 = 100*[5 5 3; 3 4 7; 5 6 3];
figure
yyaxis left
plot(x,y1)
yyaxis right
plot(x,y2)

ax = gca;
yyaxis left
ax.YDir = 'reverse';
yyaxis right
ax.YDir = 'reverse';

%% Change Numeric Ruler Properties
% Reverse the directions by setting properties of the
% numeric ruler objects. Use |ax.YAxis(1)| to refer to the left side and
% |ax.YAxis(2)| to refer to the right side.

x = [1 2 3];
y1 = [2 6 4; 3 5 4; 5 7 8];
y2 = 100*[5 5 3; 3 4 7; 5 6 3];
figure
yyaxis left
plot(x,y1)
yyaxis right
plot(x,y2)

ax = gca;
ax.YAxis(1).Direction = 'reverse';
ax.YAxis(2).Direction = 'reverse';