-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreateLegend.m
141 lines (136 loc) · 7 KB
/
createLegend.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
function createLegend(fig_handle,current_axes)
% CREATELEGEND
%
%
% Created by Megan Schroeder
% Last Modified 2014-03-18
%% Main
% Main function definition
set(fig_handle,'Units','normalized')
lPos = [0.75 0.125 0.07 0.425];
hLines = get(current_axes,'Children');
checkLine = @(x) strcmp(get(x,'Type'),'line');
hLines(~arrayfun(checkLine,hLines)) = [];
nAnnotations = length(hLines);
if nAnnotations <= 8
hOutlineBoxAnnotation = annotation('rectangle');
set(hOutlineBoxAnnotation,'Units','normalized');
set(hOutlineBoxAnnotation,'Position',[lPos(1) lPos(2) lPos(3) 0.05+nAnnotations*0.025]);
set(hOutlineBoxAnnotation,'FaceColor',[1 1 1]);
set(hOutlineBoxAnnotation,'Tag','LegendAnnotation');
hHeaderAnnotation = annotation('textbox');
set(hHeaderAnnotation,'Units','normalized');
set(hHeaderAnnotation,'Position',[lPos(1) lPos(2)+0.025+nAnnotations*0.025 lPos(3) 0.025]);
set(hHeaderAnnotation,'HorizontalAlignment','center');
set(hHeaderAnnotation,'String','Legend');
set(hHeaderAnnotation,'FontWeight','bold');
set(hHeaderAnnotation,'BackgroundColor',[0.39 0.47 0.64]);
set(hHeaderAnnotation,'Color',[1 1 1]);
set(hHeaderAnnotation,'EdgeColor',[0 0 0]);
set(hHeaderAnnotation,'Tag','LegendAnnotation');
Colors = zeros(nAnnotations,3);
LineStyles = cell(nAnnotations,1);
LineWidths = zeros(nAnnotations,1);
DisplayNames = cell(nAnnotations,1);
for i = 1:nAnnotations
Colors(i,:) = get(hLines(i),'Color');
LineStyles{i} = get(hLines(i),'LineStyle');
LineWidths(i) = get(hLines(i),'LineWidth');
DisplayNames{i} = get(hLines(i),'DisplayName');
end
Colors = flipud(Colors);
LineStyles = flipud(LineStyles);
LineWidths = flipud(LineWidths);
DisplayNames = flipud(DisplayNames);
hLineAnnotations = zeros(nAnnotations,1);
for i = 1:nAnnotations
hLineAnnotations(i) = annotation('line');
set(hLineAnnotations(i),'Units','normalized');
set(hLineAnnotations(i),'Position',[lPos(1)+0.05*lPos(3) lPos(2)+0.025+nAnnotations*0.025-i*0.025 lPos(3)*0.2 0]);
set(hLineAnnotations(i),'Color',Colors(i,:));
set(hLineAnnotations(i),'LineStyle',LineStyles{i});
set(hLineAnnotations(i),'LineWidth',LineWidths(i));
set(hLineAnnotations(i),'Tag','LegendAnnotation');
end
hLabelAnnotations = zeros(nAnnotations,1);
for i = 1:nAnnotations
hLabelAnnotations(i) = annotation('textbox');
set(hLabelAnnotations(i),'Units','normalized');
set(hLabelAnnotations(i),'Position',[lPos(1)+0.3*lPos(3) lPos(2)+0.0125+nAnnotations*0.025-i*0.025 lPos(3)*0.65 0.025]);
set(hLabelAnnotations(i),'HorizontalAlignment','left');
set(hLabelAnnotations(i),'VerticalAlignment','middle');
set(hLabelAnnotations(i),'FontSize',8);
set(hLabelAnnotations(i),'String',DisplayNames{i});
set(hLabelAnnotations(i),'BackgroundColor','none');
set(hLabelAnnotations(i),'EdgeColor','none');
set(hLabelAnnotations(i),'Tag','LegendAnnotation');
end
%%%%%%%
else
lPos = [0.525 0.05 0.42 0.425];
hOutlineBoxAnnotation = annotation('rectangle');
set(hOutlineBoxAnnotation,'Units','normalized');
set(hOutlineBoxAnnotation,'Position',[lPos(1) lPos(2) lPos(3) 0.05+10*0.025]);
set(hOutlineBoxAnnotation,'FaceColor',[1 1 1]);
set(hOutlineBoxAnnotation,'Tag','LegendAnnotation');
hHeaderAnnotation = annotation('textbox');
set(hHeaderAnnotation,'Units','normalized');
set(hHeaderAnnotation,'Position',[lPos(1) lPos(2)+0.025+10*0.025 lPos(3) 0.025]);
set(hHeaderAnnotation,'HorizontalAlignment','center');
set(hHeaderAnnotation,'String','Legend');
set(hHeaderAnnotation,'FontWeight','bold');
set(hHeaderAnnotation,'BackgroundColor',[0.39 0.47 0.64]);
set(hHeaderAnnotation,'Color',[1 1 1]);
set(hHeaderAnnotation,'EdgeColor',[0 0 0]);
set(hHeaderAnnotation,'Tag','LegendAnnotation');
Colors = zeros(nAnnotations,3);
LineStyles = cell(nAnnotations,1);
LineWidths = zeros(nAnnotations,1);
DisplayNames = cell(nAnnotations,1);
for i = 1:nAnnotations
Colors(i,:) = get(hLines(i),'Color');
LineStyles{i} = get(hLines(i),'LineStyle');
LineWidths(i) = get(hLines(i),'LineWidth');
DisplayNames{i} = get(hLines(i),'DisplayName');
end
Colors = flipud(Colors);
LineStyles = flipud(LineStyles);
LineWidths = flipud(LineWidths);
DisplayNames = flipud(DisplayNames);
hLineAnnotations = zeros(nAnnotations,1);
for i = 1:nAnnotations
hLineAnnotations(i) = annotation('line');
set(hLineAnnotations(i),'Units','normalized');
if i <= 10
set(hLineAnnotations(i),'Position',[lPos(1)+0.025*lPos(3) lPos(2)+0.025+10*0.025-i*0.025 lPos(3)*0.2/4 0]);
elseif i <= 20
set(hLineAnnotations(i),'Position',[lPos(1)+(1/3+0.025)*lPos(3) lPos(2)+0.025+10*0.025-(i-10)*0.025 lPos(3)*0.2/4 0]);
else
set(hLineAnnotations(i),'Position',[lPos(1)+(2/3+0.025)*lPos(3) lPos(2)+0.025+10*0.025-(i-20)*0.025 lPos(3)*0.2/4 0]);
end
set(hLineAnnotations(i),'Color',Colors(i,:));
set(hLineAnnotations(i),'LineStyle',LineStyles{i});
set(hLineAnnotations(i),'LineWidth',LineWidths(i));
set(hLineAnnotations(i),'Tag','LegendAnnotation');
end
hLabelAnnotations = zeros(nAnnotations,1);
for i = 1:nAnnotations
hLabelAnnotations(i) = annotation('textbox');
set(hLabelAnnotations(i),'Units','normalized');
if i <= 10
set(hLabelAnnotations(i),'Position',[lPos(1)+0.1*lPos(3) lPos(2)+0.0125+10*0.025-i*0.025 lPos(3)*0.65/4 0.025]);
elseif i <= 20
set(hLabelAnnotations(i),'Position',[lPos(1)+(1/3+0.1)*lPos(3) lPos(2)+0.0125+10*0.025-(i-10)*0.025 lPos(3)*0.65/4 0.025]);
else
set(hLabelAnnotations(i),'Position',[lPos(1)+(2/3+0.1)*lPos(3) lPos(2)+0.0125+10*0.025-(i-20)*0.025 lPos(3)*0.65/4 0.025]);
end
set(hLabelAnnotations(i),'HorizontalAlignment','left');
set(hLabelAnnotations(i),'VerticalAlignment','middle');
set(hLabelAnnotations(i),'FontSize',8);
set(hLabelAnnotations(i),'String',DisplayNames{i});
set(hLabelAnnotations(i),'BackgroundColor','none');
set(hLabelAnnotations(i),'EdgeColor','none');
set(hLabelAnnotations(i),'Tag','LegendAnnotation');
end
end
end