forked from benschneider/PyGnuplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyGnuplot.py
More file actions
112 lines (89 loc) · 3.66 KB
/
PyGnuplot.py
File metadata and controls
112 lines (89 loc) · 3.66 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
'''
By Ben Schneider
Simple python wrapper for Gnuplot
Thanks to steview2000 for suggesting to separate processes,
jrbrearley for help with debugging in python 3.4+
Example:
import PyGnuplot as gp
import numpy as np
X = np.arange(10)
Y = np.sin(X/(2*np.pi))
Z = Y**2.0
gp.s([X,Y,Z]) # saves data into tmp.dat
gp.c('plot "tmp.dat" u 1:2 w lp) # send 'plot instructions to gnuplot'
gp.c('replot "tmp.dat" u 1:3' w lp)
gp.p('myfigure.ps') # creates postscript file
'''
from subprocess import Popen as _Popen, PIPE as _PIPE
default_term = 'x11' # change this if you use a different terminal
class _FigureList(object):
def __init__(self):
proc = _Popen(['gnuplot', '-p'], shell=False, stdin=_PIPE, universal_newlines=True) # persitant -p
self.instance = {0 : [proc, default_term]} # {figure number : [process, terminal type]}
self.n = 0 # currently selected Figure
# Format:
# instance[self.n][0] = process
# instance[self.n][1] = terminal
def figure(number=None):
'''Make Gnuplot plot in a new Window or update a defined one figure(num=None, term='x11'):
>>> figure(2) # would create or update figure 2
>>> figure() # simply creates a new figure
returns the new figure number
'''
if not isinstance(number, int): # create new figure if no number was given
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)
fl.instance[number] = [proc, default_term]
fl.n = number
c('set term ' + str(fl.instance[fl.n][1]) + ' ' + str(fl.n))
return number
def c(command):
'''
Send command to gnuplot
>>> c('plot sin(x)')
>>> c('plot "tmp.dat" u 1:2 w lp)
'''
proc = fl.instance[fl.n][0] # this is where the process is
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)
(assumes equal sizes and 2D data sets)
>>> s(data, filename='tmp.dat') # overwrites/creates tmp.dat
'''
file = open(filename, 'w')
columns = len(data)
rows = len(data[0])
for j in range(rows):
for i in range(columns):
file.write(str(data[i][j]))
file.write(' ')
file.write('\n')
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)
c('plot "' + filename + '" w lp')
def p(filename='tmp.ps', width=14, height=9, fontsize=12, term=default_term):
'''Script to make gnuplot print into a postscript file
>>> p(filename='myfigure.ps') # overwrites/creates myfigure.ps
'''
c('set term postscript size ' + str(width) + 'cm, ' + str(height) + 'cm color solid ' +
str(fontsize) + " font 'Calibri';")
c('set out "' + filename + '";')
c('replot;')
c('set term ' + str(term) + '; replot')
def pdf(filename='tmp.pdf', width=14, height=9, fontsize=12, term=default_term):
'''Script to make gnuplot print into a pdf file
>>> pdf(filename='myfigure.pdf') # overwrites/creates myfigure.pdf
'''
c('set term pdf enhanced size ' + str(width) + 'cm, ' + str(height) + 'cm color solid fsize ' +
str(fontsize) + " fname 'Helvetica';")
c('set out "' + filename + '";')
c('replot;')
c('set term ' + str(term) + '; replot')
fl = _FigureList()