Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions PyGnuplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class _FigureList(object):

def __init__(self):
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE, universal_newlines=True) # persitant -p
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE, stdout=_PIPE, stderr=_PIPE, universal_newlines=True) # persitant -p
self.instance = {0 : [proc, default_term]} # {figure number : [process, terminal type]}
self.n = 0 # currently selected Figure
# Format:
Expand All @@ -45,14 +45,26 @@ def figure(number=None):
number = max(fl.instance) + 1

if number not in fl.instance: # number is new
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE, universal_newlines=True)
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE, stdout=_PIPE, stderr=_PIPE, universal_newlines=True)
fl.instance[number] = [proc, default_term]

fl.n = number
c('set term ' + str(fl.instance[fl.n][1]) + ' ' + str(fl.n))
return number


def get(command):
'''
Get a variable from gnuplot
pi = gp.get('pi')
Returns a string
'''
proc = fl.instance[fl.n][0] # this is where the process is
proc.stdin.write('print ' + command + '\n')
errs, outs = proc.communicate(timeout=15)
return outs.strip()


def c(command):
'''
Send command to gnuplot
Expand All @@ -63,6 +75,7 @@ def c(command):
proc.stdin.write(command + '\n') # \n 'send return in python 2.7'
proc.stdin.flush() # send the command in python 3.4+


def s(data, filename='tmp.dat'):
'''
saves numbers arrays and text into filename (default = 'tmp.dat)
Expand All @@ -77,10 +90,11 @@ def s(data, filename='tmp.dat'):
file.write(str(data[i][j]))
file.write(' ')
file.write('\n')
if j % 1000 == 0 :
if j % 1000 == 0:
file.flush() # write once after every 1000 entries
file.close() # write the rest


def plot(data, filename='tmp.dat'):
''' Save data into filename (default = 'tmp.dat') and send plot instructions to Gnuplot'''
s(data, filename)
Expand Down