-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakePlotNicer.m
242 lines (223 loc) · 7.53 KB
/
makePlotNicer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
function makePlotNicer(specStruct)
%makePlotNicer Set a variety of parameters to improve the appearance of a
%plot within an Axes object
% makePlotNicer(specStruct) modifies the parameters indicated in the
% input structure. The allowed parameters are:
% - 'targetAxes' (Axes object)
% - 'position' (vector indicating Axes position)
% - 'txtTitle' (string indicating Axes title)
% - 'txtXlabel' (string indicating x-axis label)
% - 'txtYlabel' (string indicating y-axis label)
% - 'txtZlabel' (string indicating z-axis label)
% - 'legendArray' (cell array with legend labels)
% - 'lineHandleVector' (vector of line handles corresponding to legend
% labels)
% - 'legendLocation' (vector indicating legend position)
% - 'xGrid' (logical indicating whether the x-grid should be formatted)
% - 'yGrid' (logical indicating whether the y-grid should be formatted)
% - 'zGrid' (logical indicating whether the z-grid should be formatted)
% - 'xlim' (vector indicating the limits of the x-axis)
% - 'ylim' (vector indicating the limits of the y-axis)
% - 'zlim' (vector indicating the limits of the z-axis)
% - 'xTick' (vector indicating the x-axis tick values)
% - 'yTick' (vector indicating the y-axis tick values)
% - 'zTick' (vector indicating the z-axis tick values)
%% If no argument is provided as input generate an empty structure
if nargin==0
specStruct = struct;
end
%% If no target axes is specified then use current axes
if ~isfield(specStruct,'targetAxes')
specStruct.targetAxes = gca;
end
%% Set axes position if specified
if isfield(specStruct,'position')
set(specStruct.targetAxes,'Position',specStruct.position)
end
%% Parameters
axisLineWidth = 1;
fontSize = 28;
lineWidth = 2.5;
markerSize = 12;
%% Set line width and marker size for line objects
lineHandleArray = findobj(specStruct.targetAxes,'Type','line');
set(lineHandleArray,...
'LineWidth',lineWidth,...
'MarkerSize',markerSize)
%% Set marker size for scatter objects
scatterHandleArray = findobj(specStruct.targetAxes,'Type','scatter');
set(scatterHandleArray,'LineWidth',lineWidth,'SizeData',markerSize^2)
%% Set line width and marker size for line objects
errorbarHandleArray = findobj(specStruct.targetAxes,'Type','ErrorBar');
set(errorbarHandleArray,...
'LineWidth',1,...
'Marker','o',...
'MarkerSize',markerSize/2,...
'CapSize',markerSize/2*1.5)
for i = 1:length(errorbarHandleArray)
set(errorbarHandleArray(i),...
'MarkerEdgeColor',get(errorbarHandleArray(i),'Color')*0.7,...
'MarkerFaceColor',get(errorbarHandleArray(i),'Color'))
end
%% Set text propperties
textHandleArray = findobj(specStruct.targetAxes,'Type','text');
if ~isempty(textHandleArray)
set(textHandleArray,...
'FontName','AvantGarde',...
'FontSize',fontSize);
end
%% Set textbox propperties
% Get the handle of the hidden annotation axes
hAnnotAxes = findall(gcf,'Tag','scribeOverlay');
% Get its children handles
if ~isempty(hAnnotAxes)
hAnnotChildren = get(hAnnotAxes,'Children');
for hAnnot = hAnnotChildren'
if isprop(hAnnot,'FontSize')
hAnnot.FontSize = fontSize;
end
end
end
%% Set title
if isfield(specStruct,'txtTitle')
hTitle = title(specStruct.targetAxes,specStruct.txtTitle);
set(hTitle,...
'FontName','AvantGarde',...
'FontSize',fontSize+2,...
'FontWeight','bold',...
'Interpreter','latex');
end
%% Set xlabel
if isfield(specStruct,'txtXlabel')
hXLabel = xlabel(specStruct.targetAxes,specStruct.txtXlabel);
set(hXLabel,...
'FontName','AvantGarde',...
'FontSize',fontSize,...
'Interpreter','latex');
end
%% Set ylabel
if isfield(specStruct,'txtYlabel')
hYLabel = ylabel(specStruct.targetAxes,specStruct.txtYlabel);
set(hYLabel,...
'FontName','AvantGarde',...
'FontSize',fontSize,...
'Interpreter','latex');
end
%% Set zlabel
if isfield(specStruct,'txtZlabel')
hZLabel = zlabel(specStruct.targetAxes,specStruct.txtZlabel);
set(hZLabel,...
'FontName','AvantGarde',...
'FontSize',fontSize,...
'Interpreter','latex');
end
%% Set legend
if isfield(specStruct,'legendArray')
if ischar(specStruct.legendArray)
specStruct.legendArray = {specStruct.legendArray};
end
if isfield(specStruct,'lineHandleVector')
% Set legend corresponding to indicated lines if indicated
if isfield(specStruct,'legendLocation')
% Set legend location if specified
legend(specStruct.targetAxes,...
specStruct.lineHandleVector,specStruct.legendArray{:},...
'Interpreter','latex',...
'Location',specStruct.legendLocation,...
'FontSize',fontSize-2);
else
legend(specStruct.targetAxes,...
specStruct.lineHandleVector,specStruct.legendArray{:},...
'Interpreter','latex',...
'FontSize',fontSize-2);
end
else
if isfield(specStruct,'legendLocation')
% Set legend location if specified
legend(...
specStruct.targetAxes,specStruct.legendArray{:},...
'Interpreter','latex',...
'Location',specStruct.legendLocation,...
'FontSize',fontSize-2);
else
legend(...
specStruct.targetAxes,specStruct.legendArray{:},...
'Interpreter','latex',...
'FontSize',fontSize-2);
end
end
end
%% Set x and y grid
if ~isfield(specStruct,'xGrid') || specStruct.xGrid
set(specStruct.targetAxes,...
'Xgrid','on',...
'XMinorGrid','on')
end
if ~isfield(specStruct,'yGrid') || specStruct.yGrid
set(specStruct.targetAxes,...
'Ygrid','on',...
'YMinorGrid','on')
end
if (~isfield(specStruct,'zGrid') || specStruct.zGrid) &&...
isprop(specStruct.targetAxes,'Zgrid')
set(specStruct.targetAxes,...
'Zgrid','on',...
'ZMinorGrid','on')
end
%% Axis limits
if isfield(specStruct,'xlim')
set(specStruct.targetAxes,'XLim',specStruct.xlim);
end
if isfield(specStruct,'ylim')
set(specStruct.targetAxes,'YLim',specStruct.ylim);
end
if isfield(specStruct,'zlim')
set(specStruct.targetAxes,'ZLim',specStruct.zlim);
end
%% Axis ticks
if isfield(specStruct,'xTick')
set(specStruct.targetAxes,'XTick',specStruct.xTick)
else
set(specStruct.targetAxes,'XMinorTick','on')
end
if isfield(specStruct,'yTick')
set(specStruct.targetAxes,'YTick',specStruct.yTick)
else
set(specStruct.targetAxes,'YMinorTick','on')
end
if isfield(specStruct,'zTick')
set(specStruct.targetAxes,'ZTick',specStruct.zTick)
else
set(specStruct.targetAxes,'ZMinorTick','on')
end
%% Other settings
set(specStruct.targetAxes,...
'FontName','Helvetica',...
'FontSize',fontSize);
set(specStruct.targetAxes,...
'Box','off',...
'TickDir','out',...
'TickLength',[.02 .02],...
'XColor',[.3 .3 .3],...
'LineWidth',axisLineWidth);
% Set right-y axis if present
if ~strcmp(specStruct.targetAxes.YAxisLocation,'right')
set(specStruct.targetAxes,'YColor',[.3 .3 .3]);
end
% Set z grid if z-axis present
if isprop(specStruct.targetAxes,'ZMinorTick')
set(specStruct.targetAxes,...
'ZMinorTick','on',...
'ZColor',[.3 .3 .3])
end
%% Set colorbar properties
cObject = get(ancestor(specStruct.targetAxes,'axes'),'Colorbar');
if ~isempty(cObject)
set(cObject,'Color',[.3 .3 .3])
set(cObject.Label,...
'FontName','AvantGarde',...
'FontSize',fontSize,...
'Interpreter','latex',...
'Color',[.3 .3 .3])
end
end