www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/PlotWithTwoYAxesExample.m
%% 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. %% Create and Plot Data % Create the data. Plot |z1| versus |t| using semilogarithmic scaling. Plot % |z2| versus |t| using linear scaling. Return the two axes objects as % array |ax|. Return the two lines as |p1| and |p2|. A = 1000; a = 0.005; b = 0.005; t = 0:900; z1 = A*exp(-a*t); z2 = sin(b*t); [ax,p1,p2] = 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 object, |ax(1)|, and % the line, |p1|, 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 object, |ax(2)|, and the line, % |p2|, correspond to the second set of data. %% Label the Axes % Label the _x_-axis and the left _y_-axis by passing the first axes object % to the |xlabel| and |ylabel| functions. Label the right _y_-axis by % passing the second axes object to the |ylabel| function. xlabel(ax(1),'Time') % label x-axis ylabel(ax(1),'Semilog Plot') % label left y-axis ylabel(ax(2),'Linear Plot') % label right y-axis %% Modify Line Appearance % Change the appearance of the lines. Starting in R2014b, you can use dot % notation to set properties. If you are using an earlier release, use the % <docid:matlab_ref.f67-432995> function instead. p1.LineStyle = '--'; p1.LineWidth = 2; p2.LineWidth = 2; %% Display Grid Lines % Display the log grid associated with the left _y_-axis by passing the % first axes object to the |grid| function. grid(ax(1),'on')