Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgnuplot.lua
More file actions
Latest commit
1106 lines (1012 loc) · 30.6 KB
/
Copy pathgnuplot.lua
File metadata and controls
1106 lines (1012 loc) · 30.6 KB
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
require'paths'
localtorchstyle=[[
##### Modified version of the sample given in
##### http://www.guidolin.net/blog/files/2010/03/gnuplot
set macro
##### Color Palette by Color Scheme Designer
##### Palette URL: http://colorschemedesigner.com/#3K40zsOsOK-K-
blue_000 = "#A9BDE6" # = rgb(169,189,230)
blue_025 = "#7297E6" # = rgb(114,151,230)
blue_050 = "#1D4599" # = rgb(29,69,153)
blue_075 = "#2F3F60" # = rgb(47,63,96)
blue_100 = "#031A49" # = rgb(3,26,73)
green_000 = "#A6EBB5" # = rgb(166,235,181)
green_025 = "#67EB84" # = rgb(103,235,132)
green_050 = "#11AD34" # = rgb(17,173,52)
green_075 = "#2F6C3D" # = rgb(47,108,61)
green_100 = "#025214" # = rgb(2,82,20)
red_000 = "#F9B7B0" # = rgb(249,183,176)
red_025 = "#F97A6D" # = rgb(249,122,109)
red_050 = "#E62B17" # = rgb(230,43,23)
red_075 = "#8F463F" # = rgb(143,70,63)
red_100 = "#6D0D03" # = rgb(109,13,3)
brown_000 = "#F9E0B0" # = rgb(249,224,176)
brown_025 = "#F9C96D" # = rgb(249,201,109)
brown_050 = "#E69F17" # = rgb(230,159,23)
brown_075 = "#8F743F" # = rgb(143,116,63)
brown_100 = "#6D4903" # = rgb(109,73,3)
grid_color = "#d5e0c9"
text_color = "#222222"
my_font = "SVBasic Manual, 12"
my_export_sz = "1024,768"
my_line_width = "2"
my_axis_width = "1"
my_ps = "1"
my_font_size = "14"
# must convert font fo svg and ps
# set term svg size @my_export_sz fname my_font fsize my_font_size enhanced dynamic rounded
# set term png size @my_export_sz large font my_font
# set term jpeg size @my_export_sz large font my_font
# set term wxt enhanced font my_font
set style data linespoints
set style function lines
set pointsize my_ps
set style line 1 linecolor rgbcolor blue_050 linewidth @my_line_width pt 7
set style line 2 linecolor rgbcolor green_050 linewidth @my_line_width pt 7
set style line 3 linecolor rgbcolor red_050 linewidth @my_line_width pt 7
set style line 4 linecolor rgbcolor brown_050 linewidth @my_line_width pt 7
set style line 5 linecolor rgbcolor blue_025 linewidth @my_line_width pt 5
set style line 6 linecolor rgbcolor green_025 linewidth @my_line_width pt 5
set style line 7 linecolor rgbcolor red_025 linewidth @my_line_width pt 5
set style line 8 linecolor rgbcolor brown_025 linewidth @my_line_width pt 5
set style line 9 linecolor rgbcolor blue_075 linewidth @my_line_width pt 9
set style line 10 linecolor rgbcolor green_075 linewidth @my_line_width pt 9
set style line 11 linecolor rgbcolor red_075 linewidth @my_line_width pt 9
set style line 12 linecolor rgbcolor brown_075 linewidth @my_line_width pt 9
set style line 13 linecolor rgbcolor blue_100 linewidth @my_line_width pt 13
set style line 14 linecolor rgbcolor green_100 linewidth @my_line_width pt 13
set style line 15 linecolor rgbcolor red_100 linewidth @my_line_width pt 13
set style line 16 linecolor rgbcolor brown_100 linewidth @my_line_width pt 13
set style line 17 linecolor rgbcolor "#224499" linewidth @my_line_width pt 11
## plot 1,2,3,4,5,6,7,8,9
set style increment user
set style arrow 1 filled
## used for bar chart borders
## set style fill solid 0.5
set size noratio
set samples 300
set border 31 lw @my_axis_width lc rgb text_color
]]
local_gptable= {}
_gptable.current=nil
_gptable.defaultterm=nil
_gptable.exe=nil
_gptable.hasrefresh=true
localfunctiongetexec()
ifnot_gptable.exethen
error('gnuplot executable is not set')
end
return_gptable.exe
end
localfunctionfindos()
locals=paths.uname()
ifsands:match("Windows") then
return'windows'
elseifsands:match('Darwin') then
return'mac'
elseifsands:match('Linux') then
return'linux'
elseifsands:match('FreeBSD') then
return'freebsd'
else
return'?'
end
end
localfunctiongetfigure(n)
localn=n
ifnotnorn==nilthen
n=#_gptable+1
end
if_gptable[n] ==nilthen
_gptable[n] = {}
if_gptable.defaultterm==nilthen
error('Gnuplot terminal is not set')
end
localsilent='> /dev/null 2>&1'
ifpaths.is_win() then
silent='> nul 2>&1'
end
_gptable[n].term=_gptable.defaultterm
_gptable[n].pipe=torch.PipeFile(getexec() ..' -persist ' ..silent,'w')
end
_gptable.current=n
ifnotpaths.filep(paths.concat(paths.home,'.gnuplot')) then
_gptable[n].pipe:writeString(torchstyle..'\n\n\n')
_gptable[n].pipe:synchronize()
end
return_gptable[n]
end
localfunctiongnuplothasterm(term)
ifnot_gptable.exethen
returnfalse--error('gnuplot exe is not found, can not chcek terminal')
end
localtfni=os.tmpname()
localtfno=os.tmpname()
localfi=io.open(tfni,'w')
fi:write('set terminal\n\n')
fi:close()
os.execute(getexec() ..' < ' ..tfni..' > ' ..tfno..' 2>&1 ')
os.remove(tfni)
localtf=io.open(tfno,'r')
locals=tf:read('*l')
whilesdo
ifs:match('^.*%s+ '..term..'') then
tf:close()
os.remove(tfno)
returntrue
end
s=tf:read('*l')
end
tf:close()
os.remove(tfno)
returnfalse
end
localfunctionfindgnuplotversion(exe)
localff=io.popen(exe..' --version','r')
localss=ff:read('*l')
ff:close()
localv,vv=ss:match('(%d).(%d)')
v=tonumber(v)
vv=tonumber(vv)
returnv,vv
end
localfunctionfindgnuplotexe()
localo=findos()
locals=paths.findprogram('gnuplot')
iftype(s) =='string' ands:match('') then
s='"' ..s..'"'
end
_gptable.hasrefresh=true
do-- preserve indentation to minimize merging issues
ifsands:len() >0ands:match('gnuplot') then
localv,vv=findgnuplotversion(s)
ifv<4then
error('gnuplot version 4 is required')
end
ifv==4andvv<4then
-- try to find gnuplot44
ifo=='linux' andpaths.filep('/usr/bin/gnuplot44') then
localss='/usr/bin/gnuplot44'
v,vv=findgnuplotversion(ss)
ifv==4andvv==4then
returnss
end
end
_gptable.hasrefresh=false
print('Gnuplot package working with reduced functionality.')
print('Please install gnuplot version >= 4.4.')
end
returns
else
returnnil
end
end
end
localfunctiongetgnuplotdefaultterm(os)
ifos=='windows' then
return'windows'
elseifos=='linux' andgnuplothasterm('wxt') then
return'wxt'
elseifos=='linux' andgnuplothasterm('qt') then
return'qt'
elseifos=='linux' andgnuplothasterm('x11') then
return'x11'
elseifos=='freebsd' andgnuplothasterm('wxt') then
return'wxt'
elseifos=='freebsd' andgnuplothasterm('qt') then
return'qt'
elseifos=='freebsd' andgnuplothasterm('x11') then
return'x11'
elseifos=='mac' andgnuplothasterm('aqua') then
return'aqua'
elseifos=='mac' andgnuplothasterm('wxt') then
return'wxt'
elseifos=='mac' andgnuplothasterm('qt') then
return'qt'
elseifos=='mac' andgnuplothasterm('x11') then
return'x11'
else
print('Can not find any of the default terminals for ' ..os..'. ' ..
'You can manually set terminal by gnuplot.setterm("terminal-name")')
returnnil
end
end
localfunctionfindgnuplot()
localexe=findgnuplotexe()
localos=findos()
ifnotexethen
returnnil
end
_gptable.exe=exe
_gptable.defaultterm=getgnuplotdefaultterm(os)
end
functiongnuplot.setgnuplotexe(exe)
localoldexe=_gptable.exe
ifnotpaths.filep(exe) then
error(exe..' does not exist')
end
_gptable.exe=exe
localv,vv=findgnuplotversion(exe)
ifv<4thenerror('gnuplot version 4 is required') end
ifv==4andvv<4then
_gptable.hasrefresh=false
print('Some functionality like adding title, labels, ... will be disabled, it is better to install gnuplot version 4.4')
else
_gptable.hasrefresh=true
end
localos=findos()
localterm=getgnuplotdefaultterm(os)
ifterm==nilthen
print('You have manually set the gnuplot exe and I can not find default terminals, run gnuplot.setterm("terminal-name") to set term type')
end
end
functiongnuplot.setterm(term)
ifgnuplothasterm(term) then
_gptable.defaultterm=term
else
error('gnuplot does not seem to have this term')
end
end
localfunctiongetCurrentPlot()
if_gptable.current==nilthen
gnuplot.figure()
end
return_gptable[_gptable.current]
end
localfunctionwriteToPlot(gp,str)
localpipe=gp.pipe
pipe:writeString(str..'\n\n\n')
pipe:synchronize()
end
localfunctionrefreshPlot(gp)
ifgp.fnamethen
writeToPlot(gp,'set output "' ..gp.fname..'"')
end
writeToPlot(gp,'refresh')
ifgp.fnamethen
writeToPlot(gp,'unset output')
end
end
localfunctionwriteToCurrent(str)
writeToPlot(getCurrentPlot(),str)
end
localfunctionrefreshCurrent()
refreshPlot(getCurrentPlot())
end
-- t is the arguments for one plot at a time
localfunctiongetvars(t)
locallegend=nil
localx=nil
localy=nil
localformat=nil
localfunctionistensor(v)
returntype(v) =='userdata' andtorch.typename(v):sub(-6) =='Tensor'
end
localfunctionisstring(v)
returntype(v) =='string'
end
if#t==0then
error('empty argument list')
end
if#t>=1then
ifisstring(t[1]) then
legend=t[1]
elseifistensor(t[1]) then
x=t[1]
else
error('expecting [string,] tensor [,tensor] [,string]')
end
end
if#t>=2then
ifxandisstring(t[2]) then
format=t[2]
elseifxandistensor(t[2]) then
y=t[2]
elseiflegendandistensor(t[2]) then
x=t[2]
else
error('expecting [string,] tensor [,tensor] [,string]')
end
end
if#t>=3then
iflegendandxandistensor(t[3]) then
y=t[3]
elseiflegendandxandisstring(t[3]) then
format=t[3]
elseifxandyandisstring(t[3]) then
format=t[3]
else
error('expecting [string,] tensor [,tensor] [,string]')
end
end
if#t==4then
iflegendandxandyandisstring(t[4]) then
format=t[4]
else
error('expecting [string,] tensor [,tensor] [,string]')
end
end
legend=legendor''
format=formator''
ifnotxthen
error('expecting [string,] tensor [,tensor] [,string]')
end
ifnotythen
ifx:dim() ==2andx:size(2) ==2then
y=x:select(2,2)
x=x:select(2,1)
elseifx:dim() ==2andx:size(2) ==4andformat=='v' then
y=torch.Tensor(x:size(1),2)
xx=torch.Tensor(x:size(1),2)
y:select(2,1):copy(x:select(2,2))
y:select(2,2):copy(x:select(2,4))
xx:select(2,1):copy(x:select(2,1))
xx:select(2,2):copy(x:select(2,3))
x=xx
elseifx:dim() ==2andx:size(2) >1then
y=x[{ {}, {2,-1} }]
x=x:select(2,1)
else
y=x
x=torch.range(1,y:size(1))
end
end
ifx:dim() ~=1andx:dim() ~=2then
error('x and y dims are wrong : x = ' ..x:nDimension() ..'D y = ' ..y:nDimension() ..'D')
end
ify:size(1) ~=x:size(1) then
error('x and y dims are wrong : x = ' ..x:nDimension() ..'D y = ' ..y:nDimension() ..'D')
end
-- if x:dim() ~= y:dim() or x:nDimension() > 2 or y:nDimension() > 2 then
-- error('x and y dims are wrong : x = ' .. x:nDimension() .. 'D y = ' .. y:nDimension() .. 'D')
-- end
-- print(x:size(),y:size())
returnlegend,x,y,format
end
-- t is the arguments for one plot at a time
localfunctiongetsplotvars(t)
locallegend=nil
localx=nil
localy=nil
localz=nil
localfunctionistensor(v)
returntype(v) =='userdata' andtorch.typename(v):sub(-6) =='Tensor'
end
localfunctionisstring(v)
returntype(v) =='string'
end
if#t==0then
error('empty argument list')
end
if#t>=1then
ifisstring(t[1]) then
legend=t[1]
elseifistensor(t[1]) then
x=t[1]
else
error('expecting [string,] tensor [,tensor] [,tensor]')
end
end
if#t>=2and#t<=4then
ifxandistensor(t[2]) andistensor(t[3]) then
y=t[2]
z=t[3]
elseiflegendandistensor(t[2]) andistensor(t[3]) andistensor(t[4]) then
x=t[2]
y=t[3]
z=t[4]
elseiflegendandistensor(t[2]) then
x=t[2]
else
error('expecting [string,] tensor [,tensor] [,tensor]')
end
elseif#t>4then
error('expecting [string,] tensor [,tensor] [,tensor]')
end
legend=legendor''
ifnotxthen
error('expecting [string,] tensor [,tensor] [,tensor]')
end
ifnotzthen
z=x
x=torch.Tensor(z:size())
y=torch.Tensor(z:size())
fori=1,x:size(1) dox:select(1,i):fill(i) end
fori=1,y:size(2) doy:select(2,i):fill(i) end
end
ifx:nDimension() ~=2ory:nDimension() ~=2orz:nDimension() ~=2then
error('x and y and z are expected to be matrices x = ' ..x:nDimension() ..'D y = ' ..y:nDimension() ..'D z = '..z:nDimension() ..'D' )
end
returnlegend,x,y,z
end
localfunctiongetscatter3vars(t)
locallegend=nil
localx=nil
localy=nil
localz=nil
localfunctionistensor(v)
returntype(v) =='userdata' andtorch.typename(v):sub(-6) =='Tensor'
end
localfunctionisstring(v)
returntype(v) =='string'
end
if#t~=3and#t~=4then
error('expecting [string,] tensor, tensor, tensor')
end
ifisstring(t[1]) then
if#t~=4then
error('expecting [string,] tensor, tensor, tensor')
end
fori=2, 4do
ifnotistensor(t[i]) then
error('expecting [string,] tensor, tensor, tensor')
end
end
legend=t[1]
x=t[2]
y=t[3]
z=t[4]
elseifistensor(t[1]) then
if#t~=3then
error('expecting [string,] tensor, tensor, tensor')
end
fori=2, 3do
ifnotistensor(t[i]) then
error('expecting [string,] tensor, tensor, tensor')
end
end
x=t[1]
y=t[2]
z=t[3]
legend=''
else
error('expecting [string,] tensor, tensor, tensor')
end
assert(x:dim() ==1andy:dim() ==1andz:dim() ==1,
'x, y and z must be 1D')
assert(x:isSameSizeAs(y) andx:isSameSizeAs(z),
'x, y and z must be the same size')
returnlegend, x, y, z
end
localfunctiongetimagescvars(t)
localpalette=nil
localx=nil
localfunctionistensor(v)
returntype(v) =='userdata' andtorch.typename(v):sub(-6) =='Tensor'
end
localfunctionisstring(v)
returntype(v) =='string'
end
if#t==0then
error('empty argument list')
end
if#t>=1then
ifistensor(t[1]) then
x=t[1]
else
error('expecting tensor [,string]')
end
end
if#t==2then
ifxandisstring(t[2]) then
palette=t[2]
else
error('expecting tensor [,string]' )
end
elseif#t>2then
error('expecting tensor [,string]')
end
legend=legendor''
ifnotxthen
error('expecting tensor [,string]')
end
ifnotpalettethen
palette='gray'
end
ifx:nDimension() ~=2then
error('x is expected to be matrices x = ' ..x:nDimension() ..'D')
end
returnx,palette
end
localfunctiongnuplot_string(legend,x,y,format)
localhstr='plot '
localdstr= {''}
localcoef= {}
localvecplot= {}
localfunctiongformat(f,i)
iff~='~' andf:find('~') orf:find('acsplines') then
coef[i] =f:gsub('~',''):gsub('acsplines','')
coef[i] =tonumber(coef[i])
f='acsplines'
end
iff=='' orf=='' thenreturn''
elseiff=='+' orf=='points' thenreturn'with points'
elseiff=='.' orf=='dots' thenreturn'with dots'
elseiff=='-' orf=='lines' thenreturn'with lines'
elseiff=='+-' orf=='linespoints' thenreturn'with linespoints'
elseiff=='|' orf=='boxes' thenreturn'with boxes'
elseiff=='~' orf=='csplines' thenreturn'smooth csplines'
elseiff=='acsplines' thenreturn'smooth acsplines'
elseiff=='V' orf=='v' orf=='vectors' thenvecplot[i]=true;return'with vectors'
elsereturnf
end
end
fori=1,#legenddo
ifi>1thenhstr=hstr..' , ' end
hstr=hstr.." '-' title '" ..legend[i] .."' " ..gformat(format[i],i)
end
hstr=hstr..'\n'
fori=1,#legenddo
localxi=x[i]
localyi=y[i]
forj=1,xi:size(1) do
ifcoef[i] then
--print(i,coef)
table.insert(dstr,string.format('%g %g %g\n',xi[j],yi[j],coef[i]))
elseifvecplot[i] then
--print(xi,yi)
table.insert(dstr,string.format('%g %g %g %g\n',xi[j][1],yi[j][1],xi[j][2],yi[j][2]))
elseifyi:dim() ==1then
table.insert(dstr,string.format('%g %g\n',xi[j],yi[j]))
else
table.insert(dstr,string.format(string.rep('%g ',1+yi:size(2)) ..'\n',xi[j],unpack(yi[j]:clone():storage():totable())))
end
end
collectgarbage()
table.insert(dstr,'e\n')
end
returnhstr,table.concat(dstr)
end
localfunctiongnu_splot_string(legend,x,y,z)
localhstr=string.format('%s\n','set contour base')
hstr=string.format('%s%s\n',hstr,'set cntrparam bspline\n')
hstr=string.format('%s%s\n',hstr,'set cntrparam levels auto\n')
hstr=string.format('%s%s\n',hstr,'set style data lines\n')
hstr=string.format('%s%s\n',hstr,'set hidden3d\n')
hstr=hstr..'splot '
localdstr= {''}
localcoef
fori=1,#legenddo
ifi>1thenhstr=hstr..' , ' end
hstr=hstr.." '-'title '" ..legend[i] .."' " ..'with lines'
end
hstr=hstr..'\n'
fori=1,#legenddo
localxi=x[i]
localyi=y[i]
localzi=z[i]
forj=1,xi:size(1) do
localxij=xi[j]
localyij=yi[j]
localzij=zi[j]
fork=1,xi:size(2) do
table.insert(dstr, string.format('%g %g %g\n',xij[k],yij[k],zij[k]))
end
table.insert(dstr,'\n')
end
table.insert(dstr,'e\n')
end
returnhstr,table.concat(dstr)
end
localfunctiongnu_scatter3_string(legend, x, y, z)
localhstr=string.format('%s\n','set contour base')
hstr=string.format('%s%s\n',hstr,'set style data points\n')
hstr=string.format('%s%s\n',hstr,'set hidden3d\n')
hstr=hstr..'splot '
localdstr= {''}
localcoef
fori=1, #legenddo
ifi>1thenhstr=hstr..' , ' end
hstr=hstr.." '-'title '" ..legend[i] .."' " ..'with points'
end
hstr=hstr..'\n'
fori=1, #legenddo
localxi=x[i]
localyi=y[i]
localzi=z[i]
forj=1, xi:size(1) do
localxij=xi[j]
localyij=yi[j]
localzij=zi[j]
table.insert(dstr,
string.format('%g %g %g\n', xij, yij, zij))
end
table.insert(dstr, 'e\n')
end
returnhstr, table.concat(dstr)
end
localfunctiongnu_imagesc_string(x,palette)
localhstr=string.format('%s\n','set view map')
hstr=string.format('%s%s %s\n',hstr,'set palette',palette)
hstr=string.format('%s%s\n',hstr,'set style data linespoints')
hstr=string.format('%s%s%g%s\n',hstr,"set xrange [ -0.5 : ",x:size(2)-0.5,"] noreverse nowriteback")
hstr=string.format('%s%s%g%s\n',hstr,"set yrange [ -0.5 : ",x:size(1)-0.5,"] reverse nowriteback")
hstr=string.format('%s%s\n',hstr,"splot '-' matrix with image")
localdstr= {''}
fori=1,x:size(1) do
localxi=x[i];
forj=1,x:size(2) do
table.insert(dstr,string.format('%g ',xi[j]))
end
table.insert(dstr, string.format('\n'))
end
table.insert(dstr,string.format('e\ne\n'))
returnhstr,table.concat(dstr)
end
functiongnuplot.close(n)
ifnotnthen
n=_gptable.current
end
localgp=_gptable[n]
ifgp==nilthenreturnend
iftype(n) =='number' andtorch.typename(gp.pipe) =='torch.PipeFile' then
_gptable.current=nil
gnuplot.plotflush(n)
writeToPlot(gp, 'quit')
-- pipefile:close is buggy in TH
--gp.pipe:close()
gp.pipe=nil
gp=nil
_gptable[n] =nil
end
collectgarbage()
end
functiongnuplot.closeall()
fori,vinpairs(_gptable) do
gnuplot.close(i)
end
_gptable= {}
collectgarbage()
findgnuplot()
_gptable.current=nil
end
localfunctionfilefigure(fname,term,n)
ifnot_gptable.hasrefreshthen
print('Plotting to files is disabled in gnuplot 4.2, install gnuplot 4.4')
end
-- Check whether the output directory can be written to here - torch.PipeFile
-- has no read/write option so we can't read back any error messages from
-- gnuplot, and writing to 'non_existent_dir/plot.png' fails silently.
localoutputDir=paths.dirname(fname)
ifoutputDir=='' then
outputDir='.'
end
ifnotpaths.dirp(outputDir) then
error('cannot save to ' ..fname..': directory does not exist')
end
localgp=getfigure(n)
gp.fname=fname
gp.term=term
writeToCurrent('set term '..gp.term)
--writeToCurrent('set output \'' .. gp.fname .. '\'')
end
functiongnuplot.epsfigure(fname,n)
filefigure(fname,'postscript eps enhanced color',n)
return_gptable.current
end
functiongnuplot.svgfigure(fname,n)
filefigure(fname,'svg',n)
return_gptable.current
end
functiongnuplot.pngfigure(fname,n)
localterm=gnuplothasterm('pngcairo') and'pngcairo' or'png'
filefigure(fname,term,n)
return_gptable.current
end
functiongnuplot.pdffigure(fname,n)
localhaspdf=gnuplothasterm('pdf') orgnuplothasterm('pdfcairo')
ifnothaspdfthen
error('your installation of gnuplot does not have pdf support enabled')
end
localterm=nil
ifgnuplothasterm('pdfcairo') then
term='pdfcairo enhanced color'
else
term='pdf enhanced color'
end
filefigure(fname,term,n)
return_gptable.current
end
functiongnuplot.figprint(fname)
localsuffix=fname:match('.+%.(.+)')
localterm=nil
localhaspdf=gnuplothasterm('pdf') orgnuplothasterm('pdfcairo')
ifsuffix=='eps' then
term='postscript eps enhanced color'
elseifsuffix=='png' then
term=gnuplothasterm('pngcairo') and'pngcairo' or'png'
term=term..' size "1024,768"'
elseifsuffix=='pdf' andhaspdfthen
ifnothaspdfthen
error('your installation of gnuplot does not have pdf support enabled')
end
ifgnuplothasterm('pdfcairo') then
term='pdfcairo'
else
term='pdf'
end
term=term..' enhanced color'
else
localerrmsg='only eps and png'
ifhaspdfthen
errmsg=errmsg..' and pdf'
end
error(errmsg' for figprint')
end
writeToCurrent('set term ' ..term)
writeToCurrent('set output \''..fname..'\'')
refreshCurrent()
writeToCurrent('unset output')
writeToCurrent('set term ' .._gptable[_gptable.current].term..'' .._gptable.current..'\n')
end
functiongnuplot.figure(n, config)
config=configor {}
localgp=getfigure(n)
writeToCurrent('set term ' .._gptable[_gptable.current].term..'' .._gptable.current..'\n')
ifnotconfig.noraisethen
writeToCurrent('raise')
end
return_gptable.current
end
functiongnuplot.plotflush(n)
ifnotnthen
n=_gptable.current
end
ifnotnor_gptable[n] ==nilthen
print('no figure ' ..tostring(n))
return
end
localgp=_gptable[n]
--xprint(gp)
refreshPlot(gp)
-- if gp.fname then
-- writeToPlot(gp,'set output "' .. gp.fname .. '"')
-- writeToPlot(gp,'refresh')
-- writeToPlot(gp,'unset output')
-- end
end
localfunctiongnulplot(legend,x,y,format)
localhdr,data=gnuplot_string(legend,x,y,format)
writeToCurrent(hdr)
writeToCurrent(data)
end
localfunctiongnusplot(legend,x,y,z)
localhdr,data=gnu_splot_string(legend,x,y,z)
writeToCurrent(hdr)
writeToCurrent(data)
end
localfunctiongnuscatter3(legend, x, y, z)
localhdr, data=gnu_scatter3_string(legend, x, y, z)
writeToCurrent(hdr)
writeToCurrent(data)
end
localfunctiongnuimagesc(x,palette)
localhdr,data=gnu_imagesc_string(x,palette)
writeToCurrent(hdr)
writeToCurrent(data)
end
functiongnuplot.xlabel(label)
ifnot_gptable.hasrefreshthen
print('gnuplot.xlabel disabled')
return
end
writeToCurrent('set xlabel "' ..label..'"')
refreshCurrent()
end
functiongnuplot.ylabel(label)
ifnot_gptable.hasrefreshthen
print('gnuplot.ylabel disabled')
return
end
writeToCurrent('set ylabel "' ..label..'"')
refreshCurrent()
end
functiongnuplot.zlabel(label)
ifnot_gptable.hasrefreshthen
print('gnuplot.zlabel disabled')
return
end
writeToCurrent('set zlabel "' ..label..'"')
refreshCurrent()
end
functiongnuplot.title(label)
ifnot_gptable.hasrefreshthen
print('gnuplot.title disabled')
return
end
writeToCurrent('set title "' ..label..'"')
refreshCurrent()
end
functiongnuplot.grid(toggle)
ifnot_gptable.hasrefreshthen
print('gnuplot.grid disabled')
return
end
iftogglethen
writeToCurrent('set grid')
refreshCurrent()
else
writeToCurrent('unset grid')
refreshCurrent()
end
end
functiongnuplot.logscale(toggle)
ifnot_gptable.hasrefreshthen
print('gnuplot.logscale disabled')
return
end
iftogglethen
writeToCurrent('set logscale y')
refreshCurrent()
else
writeToCurrent('unset logscale y')
refreshCurrent()
end
end
functiongnuplot.movelegend(hloc,vloc)
ifnot_gptable.hasrefreshthen
print('gnuplot.movelegend disabled')
return
end
ifhloc~='left' andhloc~='right' andhloc~='center' then
error('horizontal location is unknown : plot.movelegend expects 2 strings as location {left|right|center}{bottom|top|middle}')
end
ifvloc~='bottom' andvloc~='top' andvloc~='middle' then
error('horizontal location is unknown : plot.movelegend expects 2 strings as location {left|right|center}{bottom|top|middle}')
end
writeToCurrent('set key ' ..hloc..'' ..vloc)
refreshCurrent()
end
functiongnuplot.axis(axis)
ifnot_gptable.hasrefreshthen
print('gnuplot.axis disabled')
return
end
ifaxis=='auto' then
writeToCurrent('set size nosquare')
writeToCurrent('set autoscale')
refreshCurrent()
elseifaxis=='image' oraxis=='equal' then
writeToCurrent('set size ratio -1')
refreshCurrent()
elseifaxis=='fill' then
writeToCurrent('set size ratio 1,1')
refreshCurrent()
elseiftype(axis) =='table' then
if#axis~=4thenprint('axis should have 4 componets {xmin,xmax,ymin,ymax}'); returnend
writeToCurrent('set xrange [' ..axis[1] ..':' ..axis[2] ..']')
writeToCurrent('set yrange [' ..axis[3] ..':' ..axis[4] ..']')
refreshCurrent()
end
end
functiongnuplot.raw(str)
writeToCurrent(str)
end
-- plot(x)
-- plot(x,'.'), plot(x,'.-')
-- plot(x,y,'.'), plot(x,y,'.-')
-- plot({x1,y1,'.'},{x2,y2,'.-'})
-- plot({{x1,y1,'.'},{x2,y2,'.-'}})
functiongnuplot.plot(...)
localarg= {...}
ifselect('#',...) ==0then
error('no inputs, expecting at least a vector')
end
localformats= {}
localxdata= {}
localydata= {}
locallegends= {}
iftype(arg[1]) =="table" then
iftype(arg[1][1]) =="table" then
arg=arg[1]
end
fori,vinipairs(arg) do
locall,x,y,f=getvars(v)
legends[#legends+1] =l
formats[#formats+1] =f
xdata[#xdata+1] =x
ydata[#ydata+1] =y
end
else
locall,x,y,f=getvars(arg)
legends[#legends+1] =l
formats[#formats+1] =f
xdata[#xdata+1] =x
ydata[#ydata+1] =y
end
gnulplot(legends,xdata,ydata,formats)