-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddAxisLeft.m
165 lines (134 loc) · 4.36 KB
/
AddAxisLeft.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function AddAxisLeft(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;
% new for R2010a
hzoom = zoom;
set(hzoom,'ActionPostCallback',@addaxis_zoom_post);
set(hzoom,'ActionPreCallback',@addaxis_zoom_pre);
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');
set(cah,'Color','none')
% 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.05;
side = 'left';
xpos = ledge-axcompleft*totwid;
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,'LineWidth',cah.LineWidth);
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,'YMinorTick',1);
set(h_ax,'YGrid',1);
set(h_ax,'Layer','top');
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
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