|
| 1 | +function addaxis(varargin) |
| 2 | +%ADDAXIS adds an axis to the current plot |
| 3 | +% you can add as many axes as you want. |
| 4 | +% |
| 5 | +% usage: |
| 6 | +% use it just like plot, except, you need to specify the abscissa |
| 7 | +% and the third input argument should be the axis limits, if at all. |
| 8 | +% |
| 9 | +% example: |
| 10 | +% |
| 11 | +% x = 0:.1:4*pi; |
| 12 | +% plot(x,sin(x)); |
| 13 | +% addaxis(x,sin(x-pi/3)); |
| 14 | +% addaxis(x,sin(x-pi/2),[-2 5],'linewidth',2); |
| 15 | +% addaxis(x,sin(x-pi/1.5),[-2 2],'v-','linewidth',2); |
| 16 | +% addaxis(x,5.3*sin(x-pi/1.3),':','linewidth',2); |
| 17 | +% |
| 18 | +% addaxislabel(1,'one'); |
| 19 | +% addaxislabel(2,'two'); |
| 20 | +% addaxislabel(3,'three'); |
| 21 | +% addaxislabel(4,'four'); |
| 22 | +% addaxislabel(5,'five'); |
| 23 | +% |
| 24 | +% addaxisplot(x,sin(x-pi/2.3)+2,3,'--','linewidth',2); |
| 25 | +% addaxisplot(x,sin(x-pi/1),5,'--','linewidth',2); |
| 26 | +% |
| 27 | +% legend('one','two','three','four','five','three-2','five-2'); |
| 28 | +% |
| 29 | +% |
| 30 | +% |
| 31 | +% Also requires AA_SPLOT.m, a modified plot function that automatically |
| 32 | +% changes colors everytime you plot. |
| 33 | +% |
| 34 | +% See also |
| 35 | +% ADDAXISPLOT, ADDAXISLABEL, AA_SPLOT |
| 36 | + |
| 37 | +% NOTE: the 'userdata' of the main axis holds a cell array for each axis |
| 38 | +% each cell holds a vector. The first element is the axis handle |
| 39 | +% the rest are handles to lines that correspond to that axis lines. |
| 40 | + |
| 41 | + |
| 42 | + % get current axis |
| 43 | + cah = gca; |
| 44 | + |
| 45 | + |
| 46 | + if nargin>=3 & ~isstr(varargin{3}) |
| 47 | + yl2 = varargin{3}; |
| 48 | + indkeep = setdiff(1:nargin,3); |
| 49 | + [varargintemp{1:nargin-1}] = deal(varargin{indkeep}); |
| 50 | + varargin = varargintemp; |
| 51 | + end |
| 52 | + |
| 53 | + % assume existing plot has axes scaled the way you want. |
| 54 | + yl = get(cah,'ylim'); |
| 55 | + cpos = get(cah,'position'); |
| 56 | + set(cah,'box','off'); |
| 57 | + |
| 58 | + % get userdata of current axis. this will hold handles to |
| 59 | + % additional axes and the handles to their corresponding plots |
| 60 | + % in the main axis |
| 61 | +% axh = get(cah,'userdata'); |
| 62 | + axh = getaddaxisdata(cah,'axisdata'); |
| 63 | + |
| 64 | + ledge = cpos(1); |
| 65 | + if length(axh)>=1 |
| 66 | + if length(axh)/2 == round(length(axh)/2) |
| 67 | + rpos = get(axh{end-1}(1),'position'); |
| 68 | + redge = rpos(1); |
| 69 | + lpos = get(axh{end}(1),'position'); |
| 70 | + ledge = lpos(1); |
| 71 | + else |
| 72 | + rpos = get(axh{end}(1),'position'); |
| 73 | + redge = rpos(1); |
| 74 | + if length(axh)>1 |
| 75 | + lpos = get(axh{end-1}(1),'position'); |
| 76 | + ledge = lpos(1); |
| 77 | + end |
| 78 | + end |
| 79 | + else |
| 80 | + redge = cpos(3)+cpos(1); |
| 81 | + ledge = cpos(1); |
| 82 | + end |
| 83 | + |
| 84 | + totwid = redge-ledge; |
| 85 | + |
| 86 | +% assume axes are added on right, then left, then right, etc. |
| 87 | + numax = length(axh)+1; |
| 88 | + |
| 89 | + |
| 90 | + % parameters setting axis separation |
| 91 | + axcompleft=0.12; |
| 92 | + if numax == 1 |
| 93 | + axcompright = 0.0; |
| 94 | + else |
| 95 | + axcompright = 0.12; |
| 96 | + end |
| 97 | + |
| 98 | + if numax/2 == round(numax/2) |
| 99 | + side = 'left'; |
| 100 | + xpos = ledge-axcompleft*totwid; |
| 101 | + else |
| 102 | + side = 'right'; |
| 103 | + xpos = redge+axcompright*totwid; |
| 104 | + end |
| 105 | + |
| 106 | + h_ax = axes('position',[xpos, cpos(2), cpos(3)*.015, cpos(4)]); |
| 107 | +% plot in new axis to get the automatically generated ylimits |
| 108 | + hplt = plot(varargin{:}); |
| 109 | + |
| 110 | + if ~exist('yl2') |
| 111 | + yl2 = get(h_ax,'ylim'); |
| 112 | + end |
| 113 | + |
| 114 | + |
| 115 | + set(h_ax,'yaxislocation',side); |
| 116 | + set(h_ax,'color',get(gcf,'color')); |
| 117 | + set(h_ax,'box','off'); |
| 118 | + set(h_ax,'xtick',[]); |
| 119 | + set(hplt,'visible','off'); |
| 120 | + |
| 121 | + set(h_ax,'ylim',yl2); |
| 122 | + |
| 123 | + |
| 124 | +% rescale all y-values |
| 125 | + y = varargin{2}; |
| 126 | + |
| 127 | + y = (y-yl2(1))./(yl2(2)-yl2(1)).*(yl(2)-yl(1))+yl(1); |
| 128 | + |
| 129 | + varargin{2} = y; |
| 130 | + axes(cah) |
| 131 | + hplts = aa_splot(varargin{:}); |
| 132 | + set(gca,'ylim',yl); |
| 133 | + |
| 134 | + % store the handles in the axis userdata |
| 135 | + axh{length(axh)+1} = [h_ax;hplts]; |
| 136 | +% set(cah,'userdata',axh); |
| 137 | + setaddaxisdata(cah,axh,'axisdata'); |
| 138 | + set(cah,'box','off'); |
| 139 | + |
| 140 | + % set the axis color if a single line was added to the plot |
| 141 | + if length(hplts)==1 |
| 142 | + set(h_ax,'ycolor',get(hplts,'color')); |
| 143 | + end |
| 144 | + |
| 145 | + % Now, compress main axis so the extra axes don't interfere |
| 146 | + % or dissappear |
| 147 | + |
| 148 | + % get axis handles |
| 149 | + axhand = cah; |
| 150 | + postot(1,:) = get(cah,'position'); |
| 151 | + for I = 1:length(axh) |
| 152 | + axhand(I+1) = axh{I}(1); |
| 153 | + postot(I+1,:) = get(axhand(I+1),'position'); |
| 154 | + end |
| 155 | + |
| 156 | + if numax/2 == round(numax/2) |
| 157 | +% side = 'left'; |
| 158 | + |
| 159 | + set(cah,'position',[postot(1,1)+axcompleft*totwid,postot(1,2), ... |
| 160 | + postot(1,3)-axcompleft*totwid, postot(1,4)]); |
| 161 | + indshift = [2:2:size(postot,1)-1]; |
| 162 | + for I = 1:length(indshift) |
| 163 | + set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)+axcompleft*totwid, ... |
| 164 | + postot(indshift(I)+1,2:end)]); |
| 165 | + end |
| 166 | + |
| 167 | + else |
| 168 | + % side = 'right'; |
| 169 | + |
| 170 | + set(cah,'position',[postot(1,1),postot(1,2),postot(1,3)-axcompright*totwid,postot(1,4)]); |
| 171 | + indshift = [1:2:size(postot,1)-1]; |
| 172 | + for I = 1:length(indshift) |
| 173 | + set(axhand(indshift(I)+1),'position',[postot(indshift(I)+1,1)-axcompright*totwid, ... |
| 174 | + postot(indshift(I)+1,2:end)]); |
| 175 | + end |
| 176 | + |
| 177 | + end |
0 commit comments