-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathm_legend.m
121 lines (102 loc) · 2.75 KB
/
m_legend.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
function [h] = m_legend(varargin)
% M_LEGEND Add a legend to a map. This function does not have the
% full functionality of LEGEND but avoids some of the problems
% encountered using that function in M_MAP. Feel free to
% add code!
%
% [legend_handle] = M_LEGEND(HANDLES,String1,String2,String3,...)
%
% puts a legend on the plot containing the handles in the vector HANDLES
% using the specified strings as labels for the corresponding handles.
% By holding the mouse button down, the legend can be moved around the
% plot.
% Original Author:: Deirdre Byrne, dbyrne@umeoce.maine.edu 00/06/23
% This software is provided "as is" without warranty of any kind.
global MAP_PROJECTION MAP_VAR_LIST
if isempty(MAP_PROJECTION),
disp('No Map Projection initialized - call M_PROJ first!');
return;
end;
if nargin < 2;
help m_legend
return
end
handles = varargin{1};
handles = handles(:);
varargin = varargin(:);
s = size(varargin,1);
if s-1 ~= size(handles,1)
disp('must have same number of legends as handles')
return
end
varargin = varargin(2:s);
% get axis location
ax=gca;
units = get(ax,'units');
p = get(ax,'pos');
c = get(ax,'color');
if strcmp(c,'none')
c = get(gcf,'color');
end
if strcmp(c,'none')
c = [0.702 0.702 0.702];
end
% calculate initial size for legend
xlen = (p(3))/5;
ylen = (p(4))/7;
% calculate position for legend
% default is to put it in bottom right corner
axpos(1) = p(1)+p(3) - xlen;
axpos(2) = p(2);
axpos(3:4) = [xlen ylen];
ButtonDownFcn = 'moveaxis';
Tag = 'legend';
h=axes('units',units,'position',axpos, ...
'visible','on', ...
'color',c, ...
'xcolor','k', ...
'ycolor','k', ...
'tag',Tag, ...
'Buttondownfcn',ButtonDownFcn, ...
'xlim',[0 1], ...
'xtick',[], ...
'ylim',[0 1], ...
'ytick',[], ...
'nextplot','add', ...
'box','on');
lh = length(handles);
% set width based on legend strings
axw = 4;
for i = 1:lh
l = length(varargin{i});
axw = max(axw,l);
end
axw = max(2*axw,axw+10);
sf = 1;
set(h,'units','characters');
axpos = get(h,'pos');
axh = (sf*lh+1);
set(h,'position',[(axpos(1)+axpos(3) - axw) axpos(2) axw axh])
for i = 1:lh
xp = [0.05 0.2 0.35];
iy = (i*sf-1)*0.8/lh + 0.25;
yp = [iy iy iy];
if strcmp(get(handles(i),'type'),'line')
mark = get(handles(i),'marker');
msize = get(handles(i),'markersize');
linest = get(handles(i),'linestyle');
linew = get(handles(i),'linewidth');
clr = get(handles(i),'color');
if ~strcmp(linest,'none')
l1(i) = plot(xp,yp,'marker','none', ...
'color',clr,'linewidth',linew);
end
l2(i) = plot(xp(2),yp(2),'marker',mark, ...
'linewidth',linew, 'markersize', msize, ...
'color',clr);
t(i) = text(0.5, iy, varargin{i});
end
end
if nargout == 0
clear h
end