-
Notifications
You must be signed in to change notification settings - Fork 5
/
plotting.m
1130 lines (1109 loc) · 42.6 KB
/
plotting.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef plotting
properties(Constant)
%NOTE: access particular fields of the default parameters with:
%out=varargs(plotting.default).isolate({'parameter1','parameter2'}).cell;
default={...
'plot_legend', {}, @iscellstr;...
'plot_legend_suppress', {}, @iscellstr;...
'plot_legend_replace', {}, @iscellstr;...
'plot_legend_location', 'best', @ischar;... %'none' turns off the legend (can't be practically done with plot_legend)
'plot_legend_fontname','FixedWidth', @ischar;...
'plot_legend_align', '', @ischar;... %empty means no alignment
'plot_legend_align_scale_bias',false, @str.islogical;... %this is done after plot_legend_align, destroying it
'plot_scale_legend_str', ' x ', @ischar;...
'plot_legend_align_str', {}, @iscellstr;...
'plot_legend_align_right_just',true, @str.islogical;...
'plot_legend_box', true, @str.islogical;...
'plot_legend_rm_dups' false, @str.islogical;...
'plot_ylabel', '', @ischar;...
'plot_xlabel', '', @ischar;...
'plot_xdate', false, @str.islogical;...
'plot_xdateformat', '', @ischar;...
'plot_xlimits', [-inf,inf],@(i) ( isnumeric(i) || isdatetime(i) || iscell(i) ) && numel(i)==2;...
'plot_ylimits', [-inf,inf],@(i) ( isnumeric(i) || iscell(i) ) && numel(i)==2;...
'plot_set_axis_limits' true, @str.islogical;...
'plot_size', 200+[0,0,21,9]*50,@(i) isnumeric(i) && numel(i)==4;...
'plot_units', 'points', @ischar;...
'plot_visible', true, @str.islogical;...
'plot_fontsize_axis', 18, @num.isscalar;...
'plot_fontsize_title', 24, @num.isscalar;...
'plot_fontsize_label', 20, @num.isscalar;...
'plot_fontsize_legend', 20, @num.isscalar;...
'plot_title', '', @ischar;...
'plot_title_default', '', @ischar;...
'plot_title_suppress', {}, @iscellstr;...
'plot_title_suffix', '', @ischar;...
'plot_title_prefix', '', @ischar;...
'plot_title_replace', {}, @iscellstr;...
'plot_grid', true, @str.islogical;...
'plot_line_width', 2, @isnumeric;...
'plot_line_style', 'none', @(i) ischar(i) || iscellstr(i);...
'plot_line_color', 'none', @(i) ischar(i) || iscellstr(i);...
'plot_line_color_order', 0, @isnumeric;...
'plot_colormap', '', @(i) (isnumeric(i) && size(i,2)==3) || ischar(i) || ishandle(i);...
'plot_psd', false, @str.islogical;...
'plot_autoscale', false, @str.islogical;... %y-scale is derived from the data (in plotting.enforce)
'plot_autoscale_factor', 4, @num.isscalar;...
'plot_automean', false, @str.islogical;... %middle-point of y axis is derived from the data (in plotting.enforce)
'plot_zeromean', false, @str.islogical;... %mean of data is removed before plotting (in simpledata.plot)
'plot_outlier_iter', 0, @num.isscalar;...
'plot_caxis', [-inf,inf], @(i) isnumeric(i) && numel(i)==2;...
'plot_logy', false, @str.islogical;...
'plot_logx', false, @str.islogical;...
'plot_pause_on_save',false, @str.islogical;...
'plot_save_fig', false, @str.islogical;...
'plot_save_mkdir', true, @str.islogical;...
'plot_save_ext', 'png', @ischar;...
'plot_dir',file.orbdir('plot'), @ischar;...
'plot_save_data', 'yes', @str.islogical;... %NOTICE: can be 'force' to recompute plot data (discards existing plot data)
'plot_force', false, @str.islogical;... %NOTICE: this turns plot_save_data to 'force' if true (and force re-plotting, but needs to be implemented where needed)
};
end
methods(Static)
%% primitives
function out=line_handles(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
tmp=get(axis_handle,'Children');
c=0;out=[];
for i=1:numel(tmp)
if isprop(tmp(i),'LineWidth')
c=c+1;
out(c)=tmp(i); %#ok<AGROW>
end
end
end
function v=common_lim(axis_handle,mode)
%convert to easier names
switch lower(mode)
case 'x'; mode='XData';
case 'y'; mode='YData';
case {'xdata','ydata'} %do nothing
otherwise; error(['Cannot handle mode ''',mode,'''.'])
end
%set output
v=[inf -inf];
%get line handles
lh=plotting.line_handles(axis_handle);
%assume no date x data
dates=false;
%loop over all lines
for i=1:numel(lh)
%get x-data
x=get(lh(i), mode );
y=get(lh(i),'YData');
%clean up nans and infs
x=x( ~isnan(y(:)) & isfinite(y(:)) & isfinite(x(:)) );
if isdatetime(x)
x=datenum(x);
dates=true;
end
%clean up NaNs (datenum above converts NaTs to NaNs, and this needs to be done after that conversion)
x=x(~isnan(x(:)));
%get x-data extremeties
mm=minmax(x);
%accumulate inclusive x-axis limits
v(1)=min([v(1),mm(1)]);
v(2)=max([v(2),mm(2)]);
end
%return datetime type
if dates
v=datetime(v,'ConvertFrom','datenum');
end
end
function out=line_color_map(mode,n)
% handle inputs
if ~exist('mode','var') || isempty(mode) || str.none(mode)
mode='spiral';
end
% translate mode
switch strrep(lower(mode),'-reversed','')
case {'flat','boring'}
branch=0;
case{'multi','many'}
branch=1;
case{'reversed','spiral'}
branch=2;
case{'jet','hsv','hot','cool','spring','summer','autumn','winter','gray','bone','copper','pink','lines'}
colormap_name=strrep(lower(mode),'-reversed','');
branch=3;
otherwise
error(['Unknown mode ''',mode,'''.'])
end
%get colororder according to branch
switch (branch)
case 0
colororder=[
0 0 1 %blue
1 0 0 %red
0 1 0 %green
0 1 1 %cyan
1 0 1 %magenta
1 1 0 %yellow
0 0 0 %black
];
case 1
% http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/#comment-12842
colororder = [
0.00 0.00 1.00
0.00 0.50 0.00
1.00 0.00 0.00
0.00 0.75 0.75
0.75 0.00 0.75
0.75 0.75 0.00
0.25 0.25 0.25
0.75 0.25 0.25
0.95 0.95 0.00
0.25 0.25 0.75
0.75 0.75 0.75
0.00 1.00 0.00
0.76 0.57 0.17
0.54 0.63 0.22
0.34 0.57 0.92
1.00 0.10 0.60
0.88 0.75 0.73
0.10 0.49 0.47
0.66 0.34 0.65
0.99 0.41 0.23
];
case 2
%number of points in the extremeties to remove
n_extrem=ceil(n*0.3);
% http://bsp.pdx.edu/Publications/2006/SPM_McNames.pdf
nc=n+2*n_extrem;
np=2.5;
% algorithm
wn = sqrt(3/8)*[0;triang(nc-2);0]; % Triangularwindow function
a12 = asin(1/sqrt(3)); % First rotation angle
a23 = pi/4; % Second rotation angle
t = linspace(sqrt(3),0,nc).'; % Independent variable
r0 = t; % Initial red values
g0 = wn.*cos(((t-sqrt(3)/2)*np*2*pi/sqrt(3))); % Initial green values
b0 = wn.*sin(((t-sqrt(3)/2)*np*2*pi/sqrt(3))); % Initial blue values
[ag,rd] = cart2pol(r0,g0); % Convert RG to polar
[r1,g1] = pol2cart(ag+a12,rd); % Rotate& convert back
b1 = b0;
[ag,rd] = cart2pol(r1,b1); % Convert RB to polar
[r2,b2] = pol2cart(ag+a23,rd); % Rotate & convert back
g2 = g1;
r = max(min(r2,1),0); % Ensure finite precision
g = max(min(g2,1),0); % effects don't exceed
b = max(min(b2,1),0); % unit cube boundaries
colororder = [r g b]; % Final colormap matrix
%cropping extremeties (avoids too dark or too light colors)
colororder([1:n_extrem,end-n_extrem+1:end],:)=[];
case 3
% colormaps name
switch strrep(mode,'-reversed','')
%some colormaps include the white color, need to remove it
case{'gray','bone','pink','hot'}
colororder=eval([colormap_name,'(',num2str(n+1),')']);
colororder=colororder(1:end-1,:);
otherwise
colororder=eval([colormap_name,'(',num2str(n),')']);
end
end
%make sure we got the right number of colors
while size(colororder,1) < n
colororder=[colororder;colororder]; %#ok<AGROW>
end
if size(colororder,1) > n
colororder=colororder(1:n,:);
end
%transform colororder to cell array
out=cell(size(colororder,1),1);
for i=1:size(colororder,1)
out{i}=colororder(i,:);
end
%enforce reverse order of colours
if contains(mode,'-reversed')
out=flipud(out);
end
%matlab orders lines in the reverse way
out=flipud(out);
end
function line_color(mode,order,axis_handle)
% PLOT_LINE_COLOR assigns different colors to all the plotted lines.
%
% By default PLOT_LINE_COLOR works on the current graphics and uses a
% colormap that shows colors that are also different inr black and white
% visualizations.
%
% PLOT_LINE_COLOR(CLR_STR) sets the color of each line to the colors
% specified in the cell array containing single characters CLR_STR.
% Recognized characters are the ones also recognized by the plot
% function. See 'help plot' for more information.
%
% PLOT_LINE_COLOR('reversed') uses the sames colors as default but in
% reverse order.
%
% PLOT_LINE_COLOR('multi') uses the colors specified at:
% http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/#comment-12842
%
% PLOT_LINE_COLOR(COLORMAP_NAME) uses the a COLORMAP_NAME to color the
% lines in the plot, see the documentation of the colormap function.
%
% PLOT_LINE_COLOR(CLR_STR,H) as before, but now will work on the figures
% with handles H.
% TODO: add copyright
% handle inputs
if ~exist('mode','var') || isempty(mode)
mode='spiral';
end
if ~exist('order','var') || isempty(order)
order = -1;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%getting lines in this axis
lines=findobj(axis_handle,'Type','line');
%enforcing
if isscalar(order)
order=1:numel(lines);
end
%flip order upside down because matlab orders lines in the reverse way
order=flipud(order(:));
if ischar(mode)
%get requested color scheme
cm=plotting.line_color_map(mode,numel(lines));
elseif iscellstr(mode)
cm=mode;
assert(numel(lines)==numel(cm),['Input list of line colors (',num2str(numel(cm)),...
') is of different from number of plot lines (',num2str(numel(lines)),').'])
else
error(['Cannot handle input ''mode'' of class ',class(mode),'.'])
end
%loop over all lines or clr entries
for i=1:min([numel(lines),numel(cm)])
set(lines(i),'Color',cm{order(i)})
end
end
function out=line_color_set_last(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
lines=findobj(axis_handle,'Type','line');
set(lines(2),'Color',get(lines(1),'Color'))
if nargout>0; out=lines(1); end
end
% function line_color_reorder(order,axis_handle)
% if ~exist('axis_handle','var') || isempty(axis_handle)
% axis_handle = gca;
% end
% lines=findobj(axis_handle,'Type','line');
% n=min([numel(order),numel(lines)]);
% colors=flipud(arrayfun(@(i) get(i,'Color'),lines,'UniformOutput',false));
% for i=1:n
% set(lines(i),'Color',colors{order(i)})
% end
% end
function line_width(w,axis_handle)
% handle inputs
if ~exist('w','var') || isempty(w)
w=2;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%loop over all axis
for ah = axis_handle(:)'
lh=plotting.line_handles(ah);
%handle scalars
if isscalar(w)
w=w*ones(size(lh));
end
for i=1:numel(lh)
set(lh(i),'LineWidth',w(i))
end
end
end
function line_markersize(w,axis_handle)
% handle inputs
if ~exist('w','var') || isempty(w)
w=12;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%loop over all axis
for ah = axis_handle(:)'
lh=plotting.line_handles(ah);
for i=1:numel(lh)
set(lh(i),'MarkerSize',w)
end
end
end
function line_style(line_str,axis_handle)
% PLOT_LINE_LINESTYLE(LINE_STR,AXIS_HANDLE) sets the linestyle of the lines in
% the specified plot.
% Created by J.Encarnacao <J.deTeixeiradaEncarnacao@tudelft.nl>
%NOTICE: marker sizes must come first (since they are always only one char).
%If there's no marker, use blank space.
if ~exist('line_str','var') || isempty(line_str)
line_styles={'-','-.','--'};
line_ticks={'o','+','d','p','s','x'};
line_str=cell(length(line_styles)*(length(line_ticks)+1),1);
line_str(1:3)={' -',' -.',' --'};
c=3;
for i=1:length(line_styles)
for j=1:length(line_ticks)
c=c+1;
line_str{c}=[line_ticks{j},line_styles{i}];
end
end
end
if ~iscellstr(line_str)
error(['expecting <line_str> to be a cell string, not a ',class(line_str),'.'])
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
lines=get(axis_handle,'Children');
%matlab orders lines in the reverse way
fix_idx=numel(lines):-1:1;
for i=1:min([numel(lines),numel(line_str)])
if ~isempty(line_str{i}) && ~isempty(line_str{i}(2:end))
set(lines(fix_idx(i)),'LineStyle',line_str{i}(2:end))
if line_str{i}(1) ~= ' '
try
set(lines(fix_idx(i)),'marker',line_str{i}(1))
catch
error('setting the marker failed. Be sure to input blank first character to lines without markers.')
end
end
end
end
end
function font_size(fs,axis_handle)
% handle inputs
if ~exist('fs','var') || isempty(fs)
fs=18;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%loop over all axis
for h = axis_handle(:)'
set( h, 'FontSize',fs);
set(get(h,'Title' ),'FontSize',round(fs*1.3));
set(get(h,'XLabel'),'FontSize',round(fs*1.1));
set(get(h,'YLabel'),'FontSize',round(fs*1.2));
end
end
function font_size_xticks(fs,axis_handle)
% handle inputs
if ~exist('fs','var') || isempty(fs)
fs=18;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%loop over all axis
for h = axis_handle(:)'
% https://www.mathworks.com/matlabcentral/answers/306225-how-can-i-change-the-font-size-of-the-tick-labels-without-changing-the-font-size-of-the-axis-labels
hl = get(h,'XLabel');
hlfs = get(hl,'FontSize');
set(get(h,'XAxis'),'FontSize', fs)
set(hl, 'FontSize', hlfs);
end
end
function font_size_yticks(fs,axis_handle)
% handle inputs
if ~exist('fs','var') || isempty(fs)
fs=18;
end
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
%loop over all axis
for h = axis_handle(:)'
% https://www.mathworks.com/matlabcentral/answers/306225-how-can-i-change-the-font-size-of-the-tick-labels-without-changing-the-font-size-of-the-axis-labels
hl = get(h,'YLabel');
hlfs = get(hl,'FontSize');
set(get(h,'YAxis'),'FontSize', fs)
set(hl, 'FontSize', hlfs);
end
end
function no_ticks(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
for i={'x','y'}
for j={'tick','ticklabel'}
set(axis_handle,[i{1},j{1}],[])
end
end
end
function size(s,fig_handle)
% handle inputs
if ~exist('s','var') || isempty(s)
s=200+[0,0,21,9]*50;
end
if ~exist('fig_handle','var') || isempty(fig_handle)
fig_handle = gcf;
end
set(fig_handle, 'Position', s,...
'PaperUnits', 'points',...
'PaperPosition', s);
end
function out=markersize(n)
out=round(50./log10(n.^2));
end
function out=xaxis_tight(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
out=plotting.common_lim(axis_handle,'x');
xlim(axis_handle,out);
end
function out=yaxis_tight(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
out=plotting.common_lim(axis_handle,'y');
ylim(axis_handle,out);
end
function out=xlim(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
out=xlim(axis_handle);
if any(~isfinite(out))
out=plotting.common_lim(axis_handle,'x');
delta=(out(2)-out(1))*0.05;
out(1)=out(1)-delta;
out(2)=out(2)+delta;
end
end
function out=ylim(axis_handle)
if ~exist('axis_handle','var') || isempty(axis_handle)
axis_handle = gca;
end
out=ylim(axis_handle);
if any(~isfinite(out))
out=plotting.common_lim(axis_handle,'y');
delta=(out(2)-out(1))*0.05;
out(1)=out(1)-delta;
out(2)=out(2)+delta;
end
end
function out=text(x,y,msg,varargin)
% add input arguments to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
'axis_handle', gca, @(i) ~isempty(i) && ishandle(i);...
'lines_y', 15, @(i) @num.isscalar;...
'lines_x', 15, @(i) @num.isscalar;...
'fontsize', 12, @(i) @num.isscalar;...
}},varargin{:});
%get axis
a=axis;
%need this to show text
line_x = (a(2)-a(1))/v.lines_x;
corner_x = a(1)+line_x;
line_y = (a(4)-a(3))/v.lines_y;
corner_y = a(4)-line_y;
out=text(corner_x+(x-1)*line_x,corner_y-(y-1)*line_y,msg,'FontSize',v.fontsize,'fontname','FixedWidth');
end
function out=stats(dat,varargin)
% add input arguments to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
'format', '%.3g', @(i) ~isempty(i) && ischar(i);...
'x_pos', 1, @(i) @num.isscalar;...
'y_pos', 0, @(i) @num.isscalar;...
'stat_modes',{'std','mean','rms','min','max','numel'}, @iscellstr;...
}},varargin{:});
out=cell(size(v.stat_modes));
good_idx=~isnan(dat);
for i=1:numel(v.stat_modes)
fh=str2func(v.stat_modes{i});
out{i}=plotting.text(v.x_pos,v.y_pos+i,...
[str.tabbed(v.stat_modes{i},5,true),' : ',num2str(fh(dat(good_idx)),v.format)]...
);
end
end
%% utility
function fig_handle=figure(varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default},varargin{:});
%outputs
fig_handle=figure('visible',str.logical(v.plot_visible,'onoff'));
%plot size
% set(fig_handle, 'Position', v.plot_size,...
% 'PaperUnits', v.plot_units,...
% 'PaperPosition', v.plot_size);
set(fig_handle, 'Position', v.plot_size,...
'PaperUnits', v.plot_units,...
'PaperSize', [v.plot_size(3)-v.plot_size(1),v.plot_size(4)-v.plot_size(2)],...
'PaperPositionMode','auto',...
'color','w');
end
function out=enforce(varargin)
% add input arguments to collection of parameters 'v'
v=varargs.wrap('sources',{
plotting.default,...
{...
'axis_handle', gca, @(i) ~isempty(i) && ishandle(i);...
}...
},varargin{:});
%resolve logicals
v.plot_autoscale=str.logical(v.plot_autoscale);
v.plot_automean =str.logical(v.plot_automean);
% sanity
if any(isfinite(v.plot_ylimits)) && ( v.plot_autoscale || v.plot_automean )
error('option ''ylimits'' and ''autoscale'' or ''automean'' do not work concurrently.')
end
%outputs
out.axis_handle=v.axis_handle;
% enforce fontsize and paper size
set( out.axis_handle, 'FontSize',v.plot_fontsize_axis)
set(get(out.axis_handle,'Title' ),'FontSize',v.plot_fontsize_title);
set(get(out.axis_handle,'XLabel'),'FontSize',v.plot_fontsize_label);
set(get(out.axis_handle,'YLabel'),'FontSize',v.plot_fontsize_label);
%enforce legend
out.legend_handle=plotting.legend(varargin{:});
% enforce line properties
line_handles=plotting.line_handles(out.axis_handle);
if ~str.none(v.plot_line_width)
plotting.line_width(v.plot_line_width,out.axis_handle)
end
% for i=1:numel(line_handles)
% set(line_handles(i),'LineWidth',v.plot_line_width)
% end
if ~str.none(v.plot_line_style)
plotting.line_style(v.plot_line_style,out.axis_handle)
end
if ~str.none(v.plot_line_color)
plotting.line_color(v.plot_line_color,v.plot_line_color_order,out.axis_handle)
end
% start with x axis
ax=plotting.xlim(out.axis_handle);
% ensure numeric values
v.plot_xlimits=cells.c2m(v.plot_xlimits);
%check if dates are requested
if (str.logical(v.plot_xdate) && ~str.logical(v.plot_psd)) || ...
strcmp(v.plot_xlabel,'time') || ...
strcmp(get(get(gca,'Xlabel'),'String'),'time')
% enforce (possible) requested x-limits
for i=1:2
if isfinite(v.plot_xlimits(i))
%NOTICE: this is needed because earlier versions of matlab
%could not handle datetime in the x labels
switch class(ax)
case 'datetime'
ax(i)=v.plot_xlimits(i);
case 'double'
ax(i)=datenum(v.plot_xlimits(i));
otherwise
error(['Cannot handle variable "ax" with class ',class(ax),...
'; implementation needed'])
end
end
end
% set auto x-label, unless one is explicity given
if strcmp(v.plot_xlabel,'time')
if ~strcmp(datestr(ax(1),'yyyymmdd'),datestr(ax(2),'yyyymmdd')) && ...
(~strcmp(datestr(ax(2),'HHMMSS'),'000000') || ax(2)-ax(1)>1)
out.xlabel_handle=xlabel(out.axis_handle,...
[datestr(ax(1),'yyyy-mm-dd'),' to ',datestr(ax(2),'yyyy-mm-dd')]...
);
else
out.xlabel_handle=xlabel(out.axis_handle,datestr(ax(1)));
end
end
%enforce requested x date tick format
if ~str.none(v.plot_xdateformat)
if ~isempty(v.plot_xdateformat)
datetick(out.axis_handle,'x',v.plot_xdateformat)
else
datetick(out.axis_handle,'x')
end
end
else
% enforce (possible) requested x-limits
for i=1:2
if isfinite(v.plot_xlimits(i)) && isnumeric(v.plot_xlimits(i))
ax(i)=v.plot_xlimits(i);
end
end
end
if ~str.none(v.plot_set_axis_limits)
% set axis limits (can be the ones matlab so wisely set)
xlim(out.axis_handle,ax);
end
% enforce requested y-limits
ay=plotting.ylim(out.axis_handle);
% ensure numeric values
v.plot_xlimits=cells.c2m(v.plot_xlimits);
for i=1:2
if isfinite(v.plot_ylimits(i))
ay(i)= v.plot_ylimits(i);
end
end
% enforce auto-scale and/or auto-mean
% TODO: add detrending here (to go along plot_outlier_iter, as is done in simpledata.outlier)
if v.plot_automean || v.plot_autoscale || v.plot_outlier_iter>0
%gather plotted data
dat=cell(size(line_handles));
for i=1:numel(line_handles)
tmp=get(line_handles(i),'ydata');
dat{i}=transpose(tmp(:));
end
dat=[dat{:}];
dat=dat(~isnan(dat(:)));
%remove outliers before computing axis limits (if requested)
for c=1:v.plot_outlier_iter
dat=simpledata.rm_outliers(dat);
end
dat=dat(~isnan(dat(:)));
if ~isempty(dat) && diff(minmax(dat))~=0
%enforce data-driven mean and/or scale
if v.plot_automean && v.plot_autoscale
ay=mean(dat)+v.plot_autoscale_factor*std(dat)*[-1,1];
elseif v.plot_automean
ay=minmax(dat);
elseif v.plot_autoscale
ay=mean(ay)+v.plot_autoscale_factor*std(dat)*[-1,1];
end
%fix non-negative data sets
if all(dat>=0) && ay(2)<0
ay(2)=0;
end
end
end
% set axis limits (can be the ones matlab so wisely set)
ylim(out.axis_handle,ay);
%enforce labels
if isempty(v.plot_xlabel)
%do nothing
elseif ~str.none(v.plot_xlabel)
out.xlabel_handle=xlabel(out.axis_handle,v.plot_xlabel);
else
% if ~isfield(out,'xlabel_handle')
out.xlabel_handle=[];
% end
xlabel('')
end
if isempty(v.plot_ylabel)
%do nothing
elseif ~str.none(v.plot_ylabel)
out.ylabel_handle=ylabel(out.axis_handle,v.plot_ylabel);
else
% if ~isfield(out,'ylabel_handle')
out.ylabel_handle=[];
% end
ylabel('')
end
%enforce title
if str.none(v.plot_title)
out.title_handle=[];
title('')
else
if str.default(v.plot_title)
title_str=v.plot_title_default;
else
title_str=v.plot_title;
end
%operate and put it there
out.plot_title=plotting.title_replace_clean(varargin{:},'plot_title',title_str);
out.title_handle=title(out.plot_title);
end
%enforce grid
if str.logical(v.plot_grid)
grid(out.axis_handle,'on')
end
%enforce colormap
if str.none(v.plot_colormap)
out.colormap=[];
else
%enforce caxis limits
if any(isfinite(v.plot_caxis))
ca=caxis;
for i=1:numel(ca)
if isfinite(v.plot_caxis(i)); ca(i)=v.plot_caxis(i);end
end
caxis(ca);
end
if ischar(v.plot_colormap) && ~isempty(v.plot_colormap)
%get list of options for the colormap: these can be added to the names of generic colormaps, e.g.:
% 'jetzero', 'optbone'
clist={'opt','zero'};
%determine if any option is being used
cuse=cellfun(@(i) str.contains(v.plot_colormap,i),clist);
%clean the colormap of options
if any(cuse); v.plot_colormap=str.clean(v.plot_colormap,clist(cuse)); end
%get the requested colormap
out.colormap=eval([v.plot_colormap,'(1000)']);
else
%any fine-tuned colormap will be distorted by the caxis command above
if any(isfinite(v.plot_colormap))
disp('WARNING: numeric plot_colormap doesn''t play well with plot_caxis for non-generic colormaps; for generic ones, you might as well pass the colormap names');
end
clist={'unknown'};
cuse=false;
%fallback to a defaule
if isempty(v.plot_colormap)
out.colormap='jet';
else
out.colormap=v.plot_colormap;
end
end
%apply options
for c=1:numel(clist)
if ~cuse(c); continue; end
switch clist{c}
case 'opt'
out.colormap=cb.opt(out.colormap,out.axis_handle);
case 'zero'
clim=caxis;
if clim(1)<0 && clim(2)>0; czero=0;
elseif all(clim<=0); czero=max(clim);
elseif all(clim>=0); czero=min(clim);
else
error('BUG TRAP: debug needed, this should not happen')
end
out.colormap=cb.zero_white(czero,out.colormap);
end
end
%apply the modified colormap
out.colormap_handle=colormap(out.axis_handle,out.colormap);
end
%enforce axis type
if str.logical(v.plot_logy)
set(gca, 'YScale', 'log')
end
if str.logical(v.plot_logx)
set(gca, 'XScale', 'log')
end
end
function out=title_replace_clean(varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
}},varargin{:});
%suppress some parts, if requested
out=strsplit(v.plot_title,{' '});
%add prefix and suffix
out=strjoin([{v.plot_title_prefix};out(:);{v.plot_title_suffix}],' ');
%replace explicit strings
if ~isempty(v.plot_title_replace)
out=str.rep(out,v.plot_title_replace{:});
end
%clean up the tile
out=str.clean(out,[v.plot_title_suppress(:);{'title'}]);
end
function out=legend_replace_clean(varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
}},varargin{:});
%propagate
out=v.plot_legend;
%replace explicit strings (keep this before the legend-cleaning bit so the strings make sense outside)
out=cellfun(@(i) str.rep(i,v.plot_legend_replace{:}),out,'UniformOutput',false);
%clean legend of _ and remove whatever is given in plot_legend_suppress
out=cellfun(@(i) str.clean(i,[v.plot_legend_suppress(:);{'title'}]),out,'UniformOutput',false);
%replace again (sometimes, it makes more sense to replace cleaned legends)
out=cellfun(@(i) str.rep(i,v.plot_legend_replace{:}),out,'UniformOutput',false);
end
function legend_handle=legend(varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
'axis_handle', gca, @(i) ~isempty(i) && ishandle(i);...
}},varargin{:});
% check if any legend is given
if str.none(v.plot_legend_location)
legend(v.axis_handle,'off')
legend_handle=[];
return
else
v.plot_legend=plotting.legend_replace_clean(varargin{:});
%maybe there's the need to align words
if ~isempty(v.plot_legend_align)
%keywords
switch lower(v.plot_legend_align)
case {'space','spaces','blank','blanks'}
v.plot_legend_align=' ';
end
v.plot_legend=str.columns(v.plot_legend,'right',v.plot_legend_align);
end
%maybe there's the need to make the legend nicely aligned
if numel(v.plot_legend_align_str)>0
I_align=numel(v.plot_legend);
J_align=numel(v.plot_legend_align_str);
%handle default right-justify
switch numel(v.plot_legend_align_right_just)
case 1
v.plot_legend_align_right_just=true(1,J_align) && str.logical(v.plot_legend_align_right_just);
case J_align+1
v.plot_legend_align_right_just=transpose(str.logical(v.plot_legend_align_right_just(:)));
otherwise
error(['plot_legend_align_right_just must have either 1 or ',num2str(numel(J_align)+1),...
' entries, not ',num2str(numel(v.plot_legend_align_right_just)),'.'])
end
%align the legend entries
legend_out=cell(I_align,J_align+1);
align_idx=cell(I_align,J_align);
%get the align indexes
for j=1:J_align
%determine locations of this align string
align_idx(:,j)=strfind(v.plot_legend,v.plot_legend_align_str{j});
%patch empty entries and correct non-empty
for i=1:I_align
if isempty(align_idx{i,j})
align_idx{i,j}=length(v.plot_legend{i});
else
align_idx{i,j}=align_idx{i,j}-1;
end
end
end
%slice the legend
for i=1:I_align
for j=1:J_align+1
switch j
case 1; i_start=1; i_stop=align_idx{i,j};
case J_align+1; i_start=align_idx{i,j-1}+1;i_stop=length(v.plot_legend{i});
otherwise; i_start=align_idx{i,j-1}+1;i_stop=align_idx{i,j};
end
legend_out{i,j}=strtrim(v.plot_legend{i}(i_start:i_stop));
end
end
%get maximum length of each legend section and pad with blanks (right-align)
for j=1:J_align+1
section_max_len=max(cellfun(@(x) length(x),legend_out(:,j)));
for i=1:I_align
legend_out{i,j}=str.tabbed(legend_out{i,j},section_max_len,v.plot_legend_align_right_just(j));
end
end
%assign column-aligned legend
for i=1:I_align
v.plot_legend{i}=strjoin(legend_out(i,:),' ');
end
end
%TODO: see if the code above can be used for the purpose below
%maybe there's the need to make the scale and bias nicely aligned
if str.logical(v.plot_legend_align_scale_bias)
%align scale and mean, if there
legend_out=cell(numel(v.plot_legend),3);
%determine locations of the scales
scale_idx=strfind(v.plot_legend,v.plot_scale_legend_str);
%patch empty entries
for i=1:numel(scale_idx)
if isempty(scale_idx{i})
scale_idx{i}=length(v.plot_legend{i});
else
scale_idx{i}=scale_idx{i}+length(v.plot_scale_legend_str);
end
end
%determine the locatios of biases
bias_idx=cell(size(scale_idx));
%patch empty entries
for i=1:numel(bias_idx)
%determine locations of the biases
bias_idx{i}=max(strfind(strtrim(v.plot_legend{i}(1:(scale_idx{i}-length(v.plot_scale_legend_str)))),' '));
if isempty(bias_idx{i})
bias_idx{i}=scale_idx{i};
else
bias_idx{i}=bias_idx{i}+1;
end
end
for i=1:numel(v.plot_legend)
legend_out{i,3}=strtrim(v.plot_legend{i}(scale_idx{i}:end));
legend_out{i,2}=strtrim(v.plot_legend{i}( bias_idx{i}:(scale_idx{i}-scale_legend_len)));
legend_out{i,1}=strtrim(v.plot_legend{i}( 1:(bias_idx{i}-1)));
end
%get maximum length of each legend section and pad with blanks (right-align)
for i=1:size(legend_out,2)
section_max_len=max(cellfun(@(x) length(x),legend_out(:,i)));
for j=1:size(legend_out,1)
legend_out{j,i}=str.tabbed(legend_out{j,i},section_max_len,true);
end
end
%assign column-aligned legend
for i=1:size(legend_out,1)
v.plot_legend{i}=strjoin(legend_out(i,:),' ');
end
end
%maybe there's the need to remove duplicate words
if str.logical(v.plot_legend_rm_dups)
v.plot_legend=cellfun(@(i) strjoin(cells.rm_empty(unique(strsplit(i)))),v.plot_legend,'UniformOutput',false);
end
end
%set the legend text (some plots don't have lines, so legends don't make sense)
warning('off','MATLAB:legend:PlotEmpty')
legend_handle=legend(v.axis_handle,v.plot_legend);
warning('on','MATLAB:legend:PlotEmpty')
%adjust the location of the legend (even if the default 'best', it may happen that it is no longer in a good position)
set(legend_handle,...
'location',v.plot_legend_location,...
'box',str.logical(v.plot_legend_box,'onoff'),...
'FontSize',v.plot_fontsize_legend,...
'FontName',v.plot_legend_fontname...
)
end
function save(filename,varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
'fig_handle', gcf, @(i) ~isempty(i) && ishandle(i);...
}},varargin{:});
if str.logical(v.plot_pause_on_save); keyboard; end
[p,n,e]=fileparts(filename);
if strcmp(e,'.') || isempty(e)
e=['.',strrep(v.plot_save_ext,'.','')];
end
if str.logical(v.plot_save_mkdir)
file.mkdir(p);
end
if str.logical(v.plot_save_fig)
saveas(v.fig_handle,fullfile(p,n),'fig')
end
saveas(v.fig_handle,fullfile(p,[n,e]))
str.say('Plotted',fullfile(p,[n,e]))
end
function [loaddata,savedata]=forcing(varargin)
% add input arguments and metadata to collection of parameters 'v'
v=varargs.wrap('sources',{plotting.default,{...
}},varargin{:});
%don't save data by default
savedata=false;
%override plot_save_data when force is true
if v.plot_force; v.plot_save_data='force'; end
%check if loading the data is possible
try
loaddata=str.logical(v.plot_save_data);
catch
switch lower(v.plot_save_data)
case 'force'
loaddata=false;
savedata=true;
otherwise
error(['Cannot handle parameter ''plot_save_data'' with value ''',v.plot_save_data,'''.'])