-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrettyFig.m
284 lines (227 loc) · 6.48 KB
/
PrettyFig.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
% PrettyFig.m
% makes current figure pretty:
% 1. makes all line widths of plots 2
% 2. turns on all minor ticks
% 3. makes all font sizes bigger
% 4. makes all plots thicker
% 5. (optional) equalises Y axis in all subplots. to use: PrettyFig('EqualiseY=1;')
% 6. makes figure background white
function [] = PrettyFig(varargin)
warning off
% defaults
lw = 2; % line width of graphical elements
plw = 2; % plot line width
fs = 24; % font size
EqualiseY = 0;
EqualiseX = 0;
FixLogX = 0;
FixLogY = 0;
plot_buffer = .1; % how much should you zoom out of the data to show extremes?
% evaluate option inputs
for i = 1:nargin
eval(varargin{i})
end
% get handle to all plots in current figure
axesHandles = findall(gcf,'type','axes');
% for each axis
for i = 1:length(axesHandles)
% get the old limits
oldx = get(axesHandles(i),'XLim');
oldy = get(axesHandles(i),'YLim');
% set line width and font size
set(axesHandles(i),'FontSize',fs,'LineWidth',lw)
% rescale the X and Y limits, but only if the user has not manually specified something
ph = findall(axesHandles(i),'type','line');
xlimits = NaN(2,length(ph));
ylimits = NaN(2,length(ph));
for j = 1:length(ph)
temp=get(ph(j),'XData');
xlimits(1,j) = min(temp); xlimits(2,j) = max(temp); clear temp
temp=get(ph(j),'YData');
ylimits(1,j) = min(temp); ylimits(2,j) = max(temp); clear temp
end
clear j
minx=min(xlimits(1,:)); miny = min(ylimits(1,:));
maxx=max(xlimits(2,:)); maxy = max(ylimits(2,:));
if ~isempty(minx) && ~isempty(maxx) && ~isempty(nonzeros([maxy miny]))
rx = abs(minx-maxx); ry = abs(miny-maxy);
if rx == 0
rx = 1; minx = minx - 1; maxx = maxx + 1;
end
if ry == 0
ry == 1; miny = miny - 1; maxy = maxy + 1;
end
if strcmp(get(axesHandles(i),'XScale'),'log')
if strcmp(get(axesHandles(i),'XLimMode'),'auto')
set(axesHandles(i),'XLim',oldx)
end
else
if strcmp(get(axesHandles(i),'XLimMode'),'auto')
set(axesHandles(i),'XLim',[minx-plot_buffer*rx maxx+plot_buffer*rx])
end
end
if strcmp(get(axesHandles(i),'YScale'),'log')
if strcmp(get(axesHandles(i),'YLimMode'),'auto')
set(axesHandles(i),'YLim',oldy)
end
else
if strcmp(get(axesHandles(i),'YLimMode'),'auto')
set(axesHandles(i),'YLim',[miny-plot_buffer*ry maxy+plot_buffer*ry])
end
end
end
% turn the minor ticks on
set(axesHandles(i),'XMinorTick','on','YMinorTick','on')
% there should be more than 1 xtick when we have a log scale
if length(get(axesHandles(i),'XTick')) == 1 && strcmp(get(axesHandles(i),'XScale'),'log')
c=get(axesHandles(i),'Children');
minlog = Inf;maxlog = -Inf;
for k = 1:length(c)
minlog = min([ min(nonzeros(get(c(1),'XData'))) minlog]);
maxlog = max([ max(nonzeros(get(c(1),'XData'))) maxlog]);
end
a = ceil(log10(minlog));
z = floor(log10(maxlog));
if length(a:z) > 2
set(axesHandles(i),'XTick',10.^(a:z));
else
end
else
end
% there should be more than 1 Ytick when we have a log scale
if length(get(axesHandles(i),'YTick')) == 1 && strcmp(get(axesHandles(i),'YScale'),'log') && FixLogY
c=get(axesHandles(i),'Children');
minlog = Inf; maxlog = -Inf;
for k = 1:length(c)
minlog = min([ min(nonzeros(get(c(k),'YData'))) minlog]);
maxlog = max([ max(nonzeros(get(c(k),'YData'))) maxlog]);
end
a = ceil(log10(minlog));
z = floor(log10(maxlog));
if length(a:z) > 2
set(axesHandles(i),'YTick',10.^(a:z));
else
% choose a smaller base
a = ceil(log(minlog));
z = floor(log(maxlog));
L = {};
for ke = a:z
%L = [L strcat('e^{',mat2str(ke),'}')];
L = [L oval(exp(ke))];
end
set(axesHandles(i),'YTick',exp(a:z),'YTickLabel',L);
end
else
end
% there should be more than 1 Xtick when we have a log scale
if length(get(axesHandles(i),'XTick')) < 3 && strcmp(get(axesHandles(i),'XScale'),'log') && FixLogX
c=get(axesHandles(i),'Children');
minlog = Inf; maxlog = -Inf;
for k = 1:length(c)
minlog = min([ min(nonzeros(abs(get(c(k),'XData')))) minlog]);
maxlog = max([ max(nonzeros(abs(get(c(k),'XData')))) maxlog]);
end
a = floor(log10(minlog));
z = ceil(log10(maxlog));
set(axesHandles(i),'XTick',10.^(a:z));
% if length(a:z) > 2
% set(axesHandles(i),'XTick',10.^(a:z));
% else
% % choose a smaller base
% a = ceil(log(minlog));
% z = floor(log(maxlog));
% L = {};
% for ke = a:z
% % L = [L strcat('e^{',mat2str(ke),'}')];
% L = [L oval(exp(ke))];
% end
% set(axesHandles(i),'XTick',exp(a:z),'XTickLabel',L);
% end
else
end
% there should be more than 1 Ytick when we have a log scale
if length(get(axesHandles(i),'YTick')) < 3 && strcmp(get(axesHandles(i),'YScale'),'log') && FixLogY
c=get(axesHandles(i),'Children');
minlog = Inf; maxlog = -Inf;
for k = 1:length(c)
minlog = min([ min(nonzeros(abs(get(c(k),'YData')))) minlog]);
maxlog = max([ max(nonzeros(abs(get(c(k),'YData')))) maxlog]);
end
a = floor(log10(minlog));
z = ceil(log10(maxlog));
set(axesHandles(i),'YTick',10.^(a:z));
% if length(a:z) > 2
% set(axesHandles(i),'XTick',10.^(a:z));
% else
% % choose a smaller base
% a = ceil(log(minlog));
% z = floor(log(maxlog));
% L = {};
% for ke = a:z
% % L = [L strcat('e^{',mat2str(ke),'}')];
% L = [L oval(exp(ke))];
% end
% set(axesHandles(i),'XTick',exp(a:z),'XTickLabel',L);
% end
else
end
% find all errorbar plots and set those line widths appropriately
ph=get(axesHandles(i),'Children');
for j = 1:length(ph)
try
set(ph(j),'LineWidth',plw)
catch
% probably an image or something.
% so reverse tick direction
set(gca,'TickDir','out')
box on
end
end
end
clear i
% find all line plots and get all their X and Y extents
ph = findall(axesHandles,'type','line');
for j = 1:length(ph)
set(ph(j),'LineWidth',plw)
end
clear j
% find all titles
th = findall(axesHandles,'type','text');
for j = 1:length(th)
set(th(j),'FontSize',fs)
end
clear j
if EqualiseY
ymin = Inf;
ymax = -Inf;
for i = 1:length(axesHandles)
yl = get(axesHandles(i),'YLim');
ymin = min([ymin yl]);
ymax = max([ymax yl]);
end
clear i
for i = 1:length(axesHandles)
set(axesHandles(i),'YLim',[ymin ymax])
end
clear i
end
if EqualiseX
xmin = Inf;
xmax = -Inf;
for i = 1:length(axesHandles)
xl = get(axesHandles(i),'XLim');
xmin = min([xmin xl]);
xmax = max([xmax xl]);
end
clear i
for i = 1:length(axesHandles)
set(axesHandles(i),'XLim',[xmin xmax])
end
clear i
end
% turn on minor ticks
for i = 1:length(axesHandles)
set(axesHandles(i),'XMinorTick','on');
end
set(gcf,'Color','w')
warning on