Anda di halaman 1dari 12

function addaxis(varargin) %ADDAXIS adds an axis to the current plot % you can add as many axes as you want.

% % usage: % use it just like plot, except, you need to specify the abscissa % and the third input argument should be the axis limits, if at all. % % example: % % x = 0:.1:4*pi; % plot(x,sin(x)); % addaxis(x,sin(x-pi/3)); % addaxis(x,sin(x-pi/2),[-2 5],'linewidth',2); % addaxis(x,sin(x-pi/1.5),[-2 2],'v-','linewidth',2); % addaxis(x,5.3*sin(x-pi/1.3),':','linewidth',2); % % addaxislabel(1,'one'); % addaxislabel(2,'two'); % addaxislabel(3,'three'); % addaxislabel(4,'four'); % addaxislabel(5,'five'); % % addaxisplot(x,sin(x-pi/2.3)+2,3,'--','linewidth',2); % addaxisplot(x,sin(x-pi/1),5,'--','linewidth',2); % % legend('one','two','three','four','five','three-2','five-2'); % % % % Also requires AA_SPLOT.m, a modified plot function that automatically % changes colors everytime you plot. % % See also % ADDAXISPLOT, ADDAXISLABEL, AA_SPLOT % % % NOTE: the 'userdata' of the main axis holds a cell array for each axis each cell holds a vector. The first element is the axis handle the rest are handles to lines that correspond to that axis lines.

% get current axis cah = gca;

if nargin>=3 & ~isstr(varargin{3}) yl2 = varargin{3}; indkeep = setdiff(1:nargin,3); [varargintemp{1:nargin-1}] = deal(varargin{indkeep}); varargin = varargintemp; end % assume existing plot has axes scaled the way you want. yl = get(cah,'ylim'); cpos = get(cah,'position'); set(cah,'box','off');

% get userdata of current axis. this will hold handles to % additional axes and the handles to their corresponding plots % in the main axis % axh = get(cah,'userdata'); axh = getaddaxisdata(cah,'axisdata'); ledge = cpos(1); if length(axh)>=1 if length(axh)/2 == round(length(axh)/2) rpos = get(axh{end-1}(1),'position'); redge = rpos(1); lpos = get(axh{end}(1),'position'); ledge = lpos(1); else rpos = get(axh{end}(1),'position'); redge = rpos(1); if length(axh)>1 lpos = get(axh{end-1}(1),'position'); ledge = lpos(1); end end else redge = cpos(3)+cpos(1); ledge = cpos(1); end totwid = redge-ledge; % assume axes are added on right, then left, then right, etc. numax = length(axh)+1;

% parameters setting axis separation axcompleft=0.12; if numax == 1 axcompright = 0.0; else axcompright = 0.12; end if numax/2 == round(numax/2) side = 'left'; xpos = ledge-axcompleft*totwid; else side = 'right'; xpos = redge+axcompright*totwid; end h_ax = axes('position',[xpos, cpos(2), cpos(3)*.015, cpos(4)]); plot in new axis to get the automatically generated ylimits hplt = plot(varargin{:}); if ~exist('yl2') yl2 = get(h_ax,'ylim'); end

set(h_ax,'yaxislocation',side); set(h_ax,'color',get(gcf,'color')); set(h_ax,'box','off'); set(h_ax,'xtick',[]); set(hplt,'visible','off'); set(h_ax,'ylim',yl2);

rescale all y-values y = varargin{2}; y = (y-yl2(1))./(yl2(2)-yl2(1)).*(yl(2)-yl(1))+yl(1); varargin{2} = y; axes(cah) hplts = aa_splot(varargin{:}); set(gca,'ylim',yl);

% store the handles in the axis userdata axh{length(axh)+1} = [h_ax;hplts]; % set(cah,'userdata',axh); setaddaxisdata(cah,axh,'axisdata'); set(cah,'box','off'); % set the axis color if a single line was added to the plot if length(hplts)==1 set(h_ax,'ycolor',get(hplts,'color')); end % % Now, compress main axis so the extra axes don't interfere or dissappear

% get axis handles axhand = cah; postot(1,:) = get(cah,'position'); for I = 1:length(axh) axhand(I+1) = axh{I}(1); postot(I+1,:) = get(axhand(I+1),'position'); end if numax/2 == round(numax/2) side = 'left'; set(cah,'position',[postot(1,1)+axcompleft*totwid,postot(1,2), ... postot(1,3)-axcompleft*totwid, postot(1,4)]); indshift = [2:2:size(postot,1)-1]; for I = 1:length(indshift) set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)+axcompleft*totwid, ... postot(indshift(I)+1,2:end)]); end else side = 'right';

set(cah,'position',[postot(1,1),postot(1,2),postot(1,3)axcompright*totwid,postot(1,4)]); indshift = [1:2:size(postot,1)-1]; for I = 1:length(indshift) set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)axcompright*totwid, ... postot(indshift(I)+1,2:end)]); end end

addaxis1
function addaxis(varargin) %ADDAXIS adds an axis to the current plot % you can add as many axes as you want. % % usage: % use it just like plot, except, you need to specify the abscissa % and the third input argument should be the axis limits, if at all. % % example: % % x = 0:.1:4*pi; % plot(x,sin(x)); % addaxis(x,sin(x-pi/3)); % addaxis(x,sin(x-pi/2),[-2 5],'linewidth',2); % addaxis(x,sin(x-pi/1.5),[-2 2],'v-','linewidth',2); % addaxis(x,5.3*sin(x-pi/1.3),':','linewidth',2); % % addaxislabel(1,'one'); % addaxislabel(2,'two'); % addaxislabel(3,'three'); % addaxislabel(4,'four'); % addaxislabel(5,'five'); % % addaxisplot(x,sin(x-pi/2.3)+2,3,'--','linewidth',2); % addaxisplot(x,sin(x-pi/1),5,'--','linewidth',2); % % legend('one','two','three','four','five','three-2','five-2'); % % % % Also requires AA_SPLOT.m, a modified plot function that automatically % changes colors everytime you plot. % % See also % ADDAXISPLOT, ADDAXISLABEL, AA_SPLOT % % % NOTE: the 'userdata' of the main axis holds a cell array for each axis each cell holds a vector. The first element is the axis handle the rest are handles to lines that correspond to that axis lines.

% get current axis cah = gca;

if nargin>=3 & ~isstr(varargin{3}) yl2 = varargin{3}; indkeep = setdiff(1:nargin,3); [varargintemp{1:nargin-1}] = deal(varargin{indkeep}); varargin = varargintemp; end % assume existing plot has axes scaled the way you want. yl = get(cah,'ylim'); cpos = get(cah,'position'); set(cah,'box','off'); % get userdata of current axis. this will hold handles to % additional axes and the handles to their corresponding plots % in the main axis % axh = get(cah,'userdata'); axh = getaddaxisdata(cah,'axisdata'); ledge = cpos(1); if length(axh)>=1 if length(axh)/2 == round(length(axh)/2) rpos = get(axh{end-1}(1),'position'); redge = rpos(1); lpos = get(axh{end}(1),'position'); ledge = lpos(1); else rpos = get(axh{end}(1),'position'); redge = rpos(1); if length(axh)>1 lpos = get(axh{end-1}(1),'position'); ledge = lpos(1); end end else redge = cpos(3)+cpos(1); ledge = cpos(1); end totwid = redge-ledge; % assume axes are added on right, then left, then right, etc. numax = length(axh)+1;

% parameters setting axis separation axcompleft=0.12; if numax == 1 axcompright = 0.0; else axcompright = 0.12; end if numax/2 == round(numax/2) side = 'left'; xpos = ledge-axcompleft*totwid;

else side = 'right'; xpos = redge+axcompright*totwid; end h_ax = axes('position',[xpos, cpos(2), cpos(3)*.015, cpos(4)]); plot in new axis to get the automatically generated ylimits hplt = plot(varargin{:}); if ~exist('yl2') yl2 = get(h_ax,'ylim'); end

set(h_ax,'yaxislocation',side); set(h_ax,'color',get(gcf,'color')); set(h_ax,'box','off'); set(h_ax,'xtick',[]); set(hplt,'visible','off'); set(h_ax,'ylim',yl2);

rescale all y-values y = varargin{2}; y = (y-yl2(1))./(yl2(2)-yl2(1)).*(yl(2)-yl(1))+yl(1); varargin{2} = y; axes(cah) hplts = aa_splot(varargin{:}); set(gca,'ylim',yl);

% store the handles in the axis userdata axh{length(axh)+1} = [h_ax;hplts]; % set(cah,'userdata',axh); setaddaxisdata(cah,axh,'axisdata'); set(cah,'box','off'); % set the axis color if a single line was added to the plot if length(hplts)==1 set(h_ax,'ycolor',get(hplts,'color')); end % % Now, compress main axis so the extra axes don't interfere or dissappear

% get axis handles axhand = cah; postot(1,:) = get(cah,'position'); for I = 1:length(axh) axhand(I+1) = axh{I}(1); postot(I+1,:) = get(axhand(I+1),'position'); end if numax/2 == round(numax/2) side = 'left';

set(cah,'position',[postot(1,1)+axcompleft*totwid,postot(1,2), ... postot(1,3)-axcompleft*totwid, postot(1,4)]); indshift = [2:2:size(postot,1)-1]; for I = 1:length(indshift) set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)+axcompleft*totwid, ... postot(indshift(I)+1,2:end)]); end else side = 'right';

set(cah,'position',[postot(1,1),postot(1,2),postot(1,3)axcompright*totwid,postot(1,4)]); indshift = [1:2:size(postot,1)-1]; for I = 1:length(indshift) set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)axcompright*totwid, ... postot(indshift(I)+1,2:end)]); end end

addaxis2
function varargout = addaxislabel(varargin) %ADDAXISLABEL adds axis labels to axes made with ADDAXIS.m % % handle_to_text = addaxislabel(axis_number, label); % % See also % ADDAXISPLOT, ADDAXIS, SPLOT if isstr(varargin{1}), axnum = varargin{2}; label = varargin{1}; else label = varargin{2}; axnum = varargin{1}; end % get current axis cah = gca; % axh = get(cah,'userdata'); axh = getaddaxisdata(cah,'axisdata'); % get axis handles axhand = cah; postot(1,:) = get(cah,'position'); for I = 1:length(axh) axhand(I+1) = axh{I}(1); postot(I+1,:) = get(axhand(I+1),'position'); end set current axis to the axis to be labeled

axes(axhand(axnum)); htxt = ylabel(label); set(htxt,'color',get(axhand(axnum),'ycolor')); % set current axis back to the main axis axes(cah); if nargout == 1 varargout{1} = htxt; end

plotyyy
function [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels) %PLOTYYY - Extends plotyy to include a third y-axis % %Syntax: [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels) % %Inputs: x1,y1 are the xdata and ydata for the first axes' line % x2,y2 are the xdata and ydata for the second axes' line % x3,y3 are the xdata and ydata for the third axes' line % ylabels is a 3x1 cell array containing the ylabel strings % %Outputs: ax 3x1 double array containing the axes' handles % hlines - 3x1 double array containing the lines' handles % %Example: %x=0:10; %y1=x; y2=x.^2; y3=x.^3; %ylabels{1}='First y-label'; %ylabels{2}='Second y-label'; %ylabels{3}='Third y-label'; %[ax,hlines] = plotyyy(x,y1,x,y2,x,y3,ylabels); %legend(hlines, 'y = x','y = x^2','y = x^3',2) % %m-files required: none %Author: Denis Gilbert, Ph.D., physical oceanography %Maurice Lamontagne Institute %Dept. of Fisheries and Oceans Canada %email: gilbertd@dfo-mpo.gc.ca %Web: http://www.qc.dfo-mpo.gc.ca/iml/ %April 2000; Last revision: 14-Nov-2001 if nargin==6 %Use empty strings for the ylabels ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' '; elseif nargin > 7 error('Too many input arguments') elseif nargin < 6 error('Not enough input arguments') end figure('units','normalized',... 'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on');

%Plot the first two lines with plotyy [ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2); cfig = get(gcf,'color'); pos = [0.1 0.1 0.7 0.8]; offset = pos(3)/5.5; %Reduce width of the two axes generated by plotyy pos(3) = pos(3) - offset/2; set(ax,'position',pos); %Determine the position of the third axes pos3=[pos(1) pos(2) pos(3)+offset pos(4)]; %Determine the proper x-limits for the third axes limx1=get(ax(1),'xlim'); limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))]; %Bug fix 14 Nov-2001: the 1.2 scale factor in the line above %was contributed by Mariano Garcia (BorgWarner Morse TEC Inc) ax(3)=axes('Position',pos3,'box','off',... 'Color','none','XColor','k','YColor','r',... 'xtick',[],'xlim',limx3,'yaxislocation','right'); hlines(3) = line(x3,y3,'Color','r','Parent',ax(3)); limy3=get(ax(3),'YLim'); %Hide unwanted portion of the x-axis line that lies %between the end of the second and third axes line([limx1(2) limx3(2)],[limy3(1) limy3(1)],... 'Color',cfig,'Parent',ax(3),'Clipping','off'); axes(ax(2)) %Label all three y-axes set(get(ax(1),'ylabel'),'string',ylabels{1}) set(get(ax(2),'ylabel'),'string',ylabels{2}) set(get(ax(3),'ylabel'),'string',ylabels{3})

plotxx
function [ax,hl1,hl2] = plotxx(x1,y1,x2,y2,xlabels,ylabels); %PLOTXX - Create graphs with x axes on both top and bottom % %Similar to PLOTYY, but ... %the independent variable is on the y-axis, %and both dependent variables are on the x-axis. % %Syntax: [ax,hl1,hl2] = plotxx(x1,y1,x2,y2,xlabels,ylabels); % %Inputs: X1,Y1 are the data for the first line (black) % X2,Y2 are the data for the second line (red) % XLABELS is a cell array containing the two x-labels % YLABELS is a cell array containing the two y-labels % %The optional output handle graphics objects AX,HL1,HL2 %allow the user to easily change the properties of the plot. %

%Example: Plot temperature T and salinity S % as a function of depth D in the ocean % %D = linspace(-100,0,50); %S = linspace(34,32,50); %T = 10*exp(D/40); %xlabels{1} = 'Temperature (C)'; %xlabels{2} = 'Salinity'; %ylabels{1} = 'Depth(m)'; %ylabels{2} = 'Depth(m)'; %[ax,hlT,hlS] = plotxx(T,D,S,D,xlabels,ylabels);

%The code is inspired from page 10-26 (Multiaxis axes) %of the manual USING MATLAB GRAPHICS, version 5. % %Tested with Matlab 5.3.1 and above on PCWIN %Author: Denis Gilbert, Ph.D., physical oceanography %Maurice Lamontagne Institute, Dept. of Fisheries and Oceans Canada %email: gilbertd@dfo-mpo.gc.ca Web: http://www.qc.dfo-mpo.gc.ca/iml/ %November 1997; Last revision: 01-Nov-2001 if nargin < 4 error('Not enough input arguments') elseif nargin==4 %Use empty strings for the xlabels xlabels{1}=' '; xlabels{2}=' '; ylabels{1}=' '; ylabels{2}=' '; elseif nargin==5 %Use empty strings for the ylabel ylabels{1}=' '; ylabels{2}=' '; elseif nargin > 6 error('Too many input arguments') end if length(ylabels) == 1 ylabels{2} = ' '; end if ~iscellstr(xlabels) error('Input xlabels must be a cell array') elseif ~iscellstr(ylabels) error('Input ylabels must be a cell array') end hl1=line(x1,y1,'Color','k'); ax(1)=gca; set(ax(1),'Position',[0.12 0.12 0.75 0.70]) set(ax(1),'XColor','k','YColor','k'); ax(2)=axes('Position',get(ax(1),'Position'),... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none',... 'XColor','r','YColor','k'); set(ax,'box','off')

hl2=line(x2,y2,'Color','r','Parent',ax(2)); %label the two x-axes set(get(ax(1),'xlabel'),'string',xlabels{1}) set(get(ax(2),'xlabel'),'string',xlabels{2}) set(get(ax(1),'ylabel'),'string',ylabels{1}) set(get(ax(2),'ylabel'),'string',ylabels{2})

plot4y
function [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels) %PLOTY4 Extends plotyy to include a third and fourth y-axis % % Syntax: [ax,hlines] = ploty4(x1,y1,x2,y2,x3,y3,x4,y4,ylabels) % % Inputs: x1,y1 are the xdata and ydata for the first axes' line % x2,y2 are the xdata and ydata for the second axes' line % x3,y3 are the xdata and ydata for the third axes' line % x4,y4 are the xdata and ydata for the fourth axes' line % ylabels is a 4x1 cell array containing the ylabel strings (optional) % % Outputs: ax 4x1 double array containing the axes' handles % hlines 4x1 double array containing the lines' handles % % Example: % x = 0:10; % y1=x; y2=x.^2; y3=x.^3; y4=x.^4; % ylabels{1} = 'First y-label'; % ylabels{2} = 'Second y-label'; % ylabels{3} = 'Third y-label'; % ylabels{4} = 'Fourth y-label'; % [ax,hlines] = ploty4(x,y1,x,y2,x,y3,x,y4,ylabels); % leghandle = legend(hlines, 'y = x','y = x^2','y = x^3','y = x^4',2); % % See also Plot, Plotyy % Based on plotyyy.m (available at www.matlabcentral.com) by : % Denis Gilbert, Ph.D.

% Check inputs msg=nargchk(8,9,nargin); error(msg); % Create figure window figure('units','normalized',... 'DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on'); %Plot the first two lines with plotyy [ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2); cfig = get(gcf,'color'); pos = [0.125 0.1 0.65 0.8]; offset = pos(3)/5.5; %Reduce width of the two axes generated by plotyy

pos(1) = pos(1) + offset; pos(3) = pos(3) - offset; set(ax,'position',pos); %Determine the position of the third/fourth axes pos3 = [pos(1) pos(2) pos(3)+offset pos(4)]; pos4 = [pos(1) - offset pos(2) pos(3)+offset pos(4)]; %Determine the proper x-limits for the third and fourth axes scale3 = pos3(3)/pos(3); scale4 = pos4(3)/pos(3); limx1 = get(ax(1),'xlim'); limx3 = [limx1(1) limx1(1)+scale3*(limx1(2)-limx1(1))]; limx4 = [limx1(2)-scale4*(limx1(2)-limx1(1)) limx1(2)]; %Create ax(3) & ax(4) ax(3) = axes('Position',pos3,'box','off',... 'Color','none','XColor',cfig,'YColor','r',... 'xtick',[],'xlim',limx3,'yaxislocation','right'); ax(4) = axes('Position',pos4,'box','off',... 'Color','none','XColor',cfig,'YColor','k',... 'xtick',[],'xlim',limx4,'yaxislocation','left'); %Plot x3,y3,x4,y4 hlines(3) = line(x3,y3,'Color','r','Parent',ax(3)); hlines(4) = line(x4,y4,'Color','k','Parent',ax(4)); %Put ax(2) on top; axes(ax(2)); %Set y-labels; if nargin==9 set(cell2mat(get(ax,{'ylabel'})),{'String'},{ylabels{:}}'); end

Anda mungkin juga menyukai