aboutsummaryrefslogtreecommitdiffstats
path: root/gnuplot.py
blob: 9b5ae11fde3d859130cee6e12a022f220b671207 (plain) (blame)
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
#!/usr/bin/env python
import defapp
from subprocess import Popen, PIPE
from optparse import make_option as o

FORMATS = ['png', 'eps', 'pdf', 'show']
STYLES  = ['points', 'lines', 'linespoints']

class CommandBuffer(object):
    def __init__(self):
        self.cmds = []

    def __call__(self, cmd):
        self.cmds += [cmd]

    def __str__(self):
        return '\n'.join([str(x) for x in self.cmds])

def gnuplot_cmd(graphs, title=None, ylabel=None, xlabel=None,
                format='show', term_opts=None,
                style='linespoints', xrange=None,
                yrange=None,
                xticks=None, yticks=None,
                key='below',
                logscale=None,
                fname=None):
    g = CommandBuffer()
    if format == 'png':
        terminal = 'png'
        if term_opts is None:
            term_opts = 'size 1024,768 large'
    elif format == 'eps':
        terminal = 'postscript eps'
        if term_opts is None:
            term_opts = 'color blacktext solid linewidth 1.0'
    elif format == 'pdf':
        terminal = 'pdf'
        if term_opts is None:
            term_opts = 'color enhanced'
    if format != 'show':
        g('set terminal %s %s' % (terminal, term_opts))
        g("set out '/dev/null'")
    if xlabel:
        g("set xlabel '%s'" % xlabel)
    if ylabel:
        g("set ylabel '%s'" % ylabel)
    if title:
        g("set title '%s'" % title)
    if xrange:
        g("set xrange [%s:%s]" % xrange)
    if yrange:
        g("set yrange [%s:%s]" % yrange)
    if xticks:
        g("set xtics %s, %s" % xticks)
    if yticks:
        g("set ytics %s, %s" % yticks)
    if logscale:
        if type(logscale) == tuple:
            for x in logscale:
                g("set logscale %s" % x)
        else:
            g("set logscale %s" % logscale)
    g('set key %s' % key)
    plot = []
    for gr in graphs:
        if type(gr) == str:
            # literal plot command
            plot.append(gr)
        elif len(gr) == 4:
            par = (gr[0], gr[1], gr[2], gr[3], style)
            plot += ["'%s' using %s:%s title '%s' with %s" % par]
        elif len(gr) == 5:
            plot += ["'%s' using %s:%s title '%s' with %s" % gr]
        elif len(gr) == 3:
            plot += ["%s title '%s' with %s" % gr]
    if plot:
        g('plot ' + ', '.join(plot))
    if format != 'show' and fname:
        g("set out '%s.%s'" % (fname, format))
    if plot:
        g('replot')
    if format != 'show' and fname:
        g('set out')
    return g

def pipe2gnuplot(cmds):
    proc = Popen(['gnuplot'], stdin=PIPE)
    proc.stdin.write(str(cmds))
    proc.stdin.close()
    proc.wait()

def gnuplot(*args, **kargs):
        cmd = gnuplot_cmd(*args, **kargs)
        pipe2gnuplot(cmd)

def eps2pdf(file):
    Popen(['ps2pdf', '-dEPSCrop', '%s.eps' % file]).wait()

options = [
    o('-f', '--format', action='store', dest='format', type='choice',
      choices=FORMATS, help='output format'),
    o('-o', '--output', action='store', dest='out', help='Output file name.'),
    o('-s', '--style', action='store', dest='style', type='choice',
      choices=STYLES, help='line style'),
    o('-t', '--title', action='store', dest='title'),
    o(None, '--xlabel', action='store', dest='xlabel'),
    o(None, '--ylabel', action='store', dest='ylabel'),
    o(None, '--xrange', action='store', dest='xrange', nargs=2, type='float'),
    o(None, '--yrange', action='store', dest='yrange', nargs=2, type='float'),
    o(None, '--xticks', action='store', dest='xticks', nargs=2, type='float'),
    o(None, '--yticks', action='store', dest='yticks', nargs=2, type='float'),
    o(None, '--term-opts', action='store', dest='term_opts'),
    ]

defaults = {
    'out'    : 'graph',
    'format' : 'show',
    'style'  : 'linespoints',
    'title'  : None,
    'xlabel' : None,
    'ylabel' : None,
    'xrange' : None,
    'yrange' : None,
    'xticks' : None,
    'yticks' : None,
    'term_opts' : None,
    }

class GnuPlotter(defapp.App):
    def __init__(self):
        defapp.App.__init__(self, options, defaults, no_std_opts=True)

    def get_cmd(self, args):
        graphs = []
        while len(args) >= 4:
            g =  args[0:4]
            graphs += [g]
            args = args[4:]
        if args:
            self.err("Warning: Ignoring trailing args. Args:", *args)
        return gnuplot_cmd(graphs,
                           title=self.options.title,
                           format=self.options.format,
                           style=self.options.style,
                           xlabel=self.options.xlabel,
                           ylabel=self.options.ylabel,
                           xrange=self.options.xrange,
                           yrange=self.options.yrange,
                           xticks=self.options.xticks,
                           yticks=self.options.yticks,
                           fname=self.options.out,
                           term_opts=self.options.term_opts)
                           
    def default(self, _):
        cmd = self.get_cmd(list(self.args))
        pipe2gnuplot(cmd)

    def do_cmd(self, _):
        cmd = self.get_cmd(self.args[1:])
        self.out(cmd)

if __name__ == "__main__":
    GnuPlotter().launch()