-
Notifications
You must be signed in to change notification settings - Fork 0
/
insetAxes.m
150 lines (131 loc) · 4.13 KB
/
insetAxes.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
function hout = insetAxes(varargin)
% insetAxes Flexible creation of inset plots. Can add an inset version of one axis onto another.
%
% insetAxes Create an empty inset axis within gca with default parameters
% (placed in upper left corner, 1/3 the size of current axis).
%
% insetAxes(param,value,...) Create an inset plot with properties according to
% parameter / value pairs.
%
% insetAxes('source',axh,...) Create inset, and copy contents of source axis
% axh into it.
%
% INPUT PARAMETERS
% source axis containing existing plot to inset. If [], or unspecified,
% create a new axis to draw into.
% location optional, corner to place inset into, ('ul','ur','ll' or 'lr'). Default 'ul'
% size optional, ratio of inset axis size to destination axis. Default 1/3.
% layer optional, 'over' (default) or 'under' controls whether inset
% is placed over or behind dest axis.
% delete delete any existing inset axes first
%
% OUTPUTS
% newax optional, handle to new inset axis
%
% Notes:
% Depends on JRI functions scaleAxis.m, getParam
% Side Effect: If layer is 'under', destaxis is modified: color set to
% 'none' so inset can show through
%
% EXAMPLES
%
% %1) simple usage
% figure
% line
% insetAxes %create new axis within first
% line; title('inset plot')
%
% %2) combine two existing plots, placing first as an inset within second
%
% %create the source of our inset
% figure
% pcolor(peaks)
% sourceAxis = gca;
%
% create the figure that accepts the inset
% figure
% logo %this might not work--seems not to create an axis?
% bigAxis %this fixes it, but need something better
% insetAxes('source',sourceAxis)
% axes(destAxis)
%
%
% See also scaleAxis.
%
%
% Free for all uses, but please retain the following:
% Original Author: John Iversen, 2006
% JRI 02/16/2006
%defaults
srcaxis = getparam('source',varargin,1);
location = getparam('location',varargin,1,'ul');
size = getparam('size',varargin,1,1/3);
layer = getparam('layer',varargin,1,'over');
doDelete = isparam('delete',varargin);
if doDelete,
delete( findobj(gcf,'tag','jri_insetAxes') );
end
if ~strmatch(lower(location),{'ul','ur','ll','lr'},'exact'),
error('JRI:insetAxes:incorrectLocation', ...
'Incorrect value for location. Must be ''ul'',''uc'',''ur'',''ll'', ''lc'' or ''lr''.')
end
%destination axis is the current axis
destfig = gcf;
destaxis = gca;
%get destination axis position
destpos = get(destaxis,'position');
[destminor, idx] = min(destpos(3:4)); %smallest dimension & identity
%find scale factor by which to reduce new axis
destsize = destminor * size; %size relative to original axis
if ~isempty(srcaxis),
newax = copyobj(srcaxis, destfig);
srcpos = get(srcaxis,'position');
srcminor = srcpos(2+idx);
scale = destsize / srcminor;
axes(newax);
scaleAxis(scale,scale);
else
srcaxis = axes;
srcpos = get(srcaxis,'position');
srcminor = srcpos(2+idx);
scale = destsize / srcminor;
newax = srcaxis;
axes(newax);
scaleAxis(scale,scale);
end
newpos = get(newax,'position');
xo=newpos(1);yo=newpos(2);
w=newpos(3);h=newpos(4);
%inset versions of axis bounds
inset = 0.05;
destleft = destpos(1) + 2*inset*destpos(3);
destright = destpos(1) + (1-inset)*destpos(3);
destbot = destpos(2) + 2*inset*destpos(4);
desttop = destpos(2) + (1-inset)*destpos(4);
switch(location(1)),
case 'u',
yo = desttop - h;
case 'l',
yo = destbot;
end
switch(location(2)),
case 'l',
xo = destleft;
case 'r',
xo = destright - w;
case 'c',
span = destright - destleft;
xo = destright - span/2 - w/2;
end
set(newax,'position',[xo yo w h])
set(newax,'tag','jri_insetAxes')
%set destaxis so we can see thru to inset
if strcmp(layer,'under')
set(destaxis,'color','none')
end
%axes(currentax)
%figure(currentfig)
if nargout,
hout = newax;
end