-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnuplot.py
143 lines (111 loc) · 3.73 KB
/
gnuplot.py
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
#!/usr/bin/python3
import sys
import os
import subprocess
import tempfile
from multiprocessing import Process
class Gnuplot() :
temp_files = [] # for deletion, if anyone cares.
def __init__( self ) :
self.gnuplot_path = Gnuplot.find_gnuplot()
#self.linestyle_id = 0
self.xss = []
self.yss = []
self.labels = []
self.style = "lines"
def open( self ) :
assert self.gnuplot_path, "gnuplot not found"
self.f = tempfile.NamedTemporaryFile( delete=False )
#self.f = subprocess.Popen( [self.gnuplot_path, "-p"], shell=False, stdin=subprocess.PIPE )
#self.write( 'set term wxt\n' ) causes error, depending on qt/wx...
def set_style( self, style ) :
self.style = style
def xlabel( self, s ) :
self.write( 'set xlabel "%s"\n' % s )
def ylabel( self, s ) :
self.write( 'set ylabel "%s"\n' % s )
def legend( self, labels ) :
self.write( 'set key outside bottom center horizontal\n' )
self.labels = labels
def plot( self, xs, ys ) :
self.xss.append( xs )
self.yss.append( ys )
def show( self ) :
# specify line styles
#self.linestyle_id += 1
#self.write( 'set style line %d \n' % self.data_id )
self.write( 'plot \\\n' )
# now a stub command... for use with other useful plottings.
self.data_plot_command()
def data_plot_command( self ) :
assert len( self.xss ) == len( self.yss )
data_str = []
for i in range( len( self.xss ) ) :
data_str.append( '"-" using 1:2 with %s title "%s" linecolor %d' % ( self.style, self.labels[i], i+1 ) )
data_str = ", ".join( data_str )
self.write( data_str )
self.write( "\n" )
for xs, ys in zip( self.xss, self.yss ) :
for x, y in zip( xs, ys ) :
self.write( '%f %f\n' % (x, y) )
self.write( 'e\n' )
def close( self ) :
#self.write( "exit\n" )
#self.f.stdin.close()
#del self.f
#p = Process( target=gnuplot_forked, args=( self.gnuplot_path, self.f.name ) )
#p.start()
self.f.close()
is_win32 = (sys.platform == 'win32')
if not is_win32:
subprocess.Popen( [self.gnuplot_path, "-p", self.f.name], shell=False )
else :
subprocess.Popen( [self.gnuplot_path, "-p", self.f.name], shell=False )
Gnuplot.temp_files.append( self.f.name )
# launch gnuplot will get rid of the tmp file afterwards.
# Tempfile, on win7, is here:
# C:\Users\USERID\AppData\Local\Temp
# There's a historical reason of using write function.
# In pipes, I have to use write, not print().
# But now that I've moved away from pipe and using temp files instead on windows,
# I can start moving away from this write function, if I want to, but that's tedius.
def write( self, cmd ) :
#self.f.stdin.write( bytes( cmd, "UTF-8" ) )
#self.f.stdin.flush()
self.f.write( bytes( cmd, "UTF-8" ) )
def find_gnuplot() :
is_win32 = (sys.platform == 'win32')
if not is_win32:
gnuplot = "/usr/bin/gnuplot"
if os.path.isfile( gnuplot ) :
return gnuplot
else:
# example for windows
import winreg
try :
key = winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\wgnuplot.exe" )
gnuplot = winreg.QueryValue( key, None )
if not os.path.isfile( gnuplot ) :
return None
else :
# I want to try pgnuplot, gnuplot ...
# until it is neat.
path = os.path.dirname( gnuplot )
gnuplot = os.path.join( path, "wgnuplot.exe" ) # piped gnuplot!
return gnuplot
except FileNotFoundError :
return None
return None
def debug_main() :
plt = Gnuplot()
print( "found gnuplot:", plt.gnuplot_path )
plt.open()
plt.write('set xrange [0:10]; set yrange [-2:2]\n')
plt.write('plot sin(x)\n')
plt.close()
# then delete Gnuplot.temp_files...
def gnuplot_forked( gnuplot, fname ) :
subprocess.call( [gnuplot, fname] )
os.unlink( fname )
if __name__ == "__main__" :
debug_main()