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

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

TERMINALS = {
    'png' : 'png',
    'eps' : 'postscript eps',
    'ps'  : 'postscript',
    'pdf' : 'pdf',
    }

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])


class Label(object):
    def __init__(self, x, y, txt):
        self.x   = x
        self.y   = y
        self.txt = txt
        # support more label stuff optionally...

    def gnuplot_cmd(self):
        return '"%s" at %f,%f center' % (self.txt, self.x, self.y)

    def __str__(self):
        return 'set label %s' % self.gnuplot_cmd()


class FileGraph(object):
    def __init__(self, fname, xcol=1, ycol=2, error=None,
                 title=None, style=None, smooth=None):
        self.fname = fname
        self.xcol  = xcol
        self.ycol  = ycol
        self.error = error
        self.title = title
        self.style = style # replace with linetype, lines/points etc.
        self.smooth = smooth

    def __str__(self):
        return self.gnuplot_cmd()

    def gnuplot_cmd(self, default_style=None):
        if self.error:
            using_txt = "%s:%s:%s" % (self.xcol, self.ycol, self.error)
            style     = self.style if self.style else "errorbars"
        else:
            using_txt = "%s:%s" % (self.xcol, self.ycol)
            style     = self.style if self.style else default_style
        style_txt = " with %s" % style if style else ""
        title_txt = ' title "%s"' % self.title if self.title else ""
        smooth_txt = ' smooth %s' % self.smooth if self.smooth else ""
        return "'%s' using %s%s%s%s" % \
            (self.fname, using_txt, title_txt, style_txt, smooth_txt)


class LiteralGraph(object):
    def __init__(self, literal_expr, title=None, style=None):
        self.expr  = literal_expr
        self.title = title
        self.style = style

    def __str__(self):
        return self.gnuplot_cmd()

    def gnuplot_cmd(self, default_style=None):
        style     = self.style if self.style else default_style
        style_txt = " with %s" % style if style else ""
        title_txt = " title '%s'" % self.title if self.title else ""
        return "%s %s%s" % \
            (self.expr, title_txt, style_txt)


label = Label

def curve(fname=None, literal=None, **kargs):
    if fname:
        return FileGraph(fname, **kargs)
    elif literal:
        return LiteralGraph(literal, **kargs)


class Plot(object):
    def __init__(self):
        self.format  = None
        self.output  = None
        self.size    = None

        self.font      = None
        self.font_size = None
        self.enhanced_text = None

        self.monochrome   = None
        self.dashed_lines = None
        self.rounded_caps = None

        self.xrange = None
        self.xticks = None
        self.xlog   = None
        self.xlabel = None

        self.yrange = None
        self.yticks = None
        self.ylog   = None
        self.ylabel = None

        self.key    = None
        self.title  = None

        self.labels = []
        self.curves = []

        self.default_style = None # for plotted curves
        self.line_styles = []

    def gnuplot_commands(self, cmd_buf=None):
        if cmd_buf:
            g = cmd_buf
        else:
            g = CommandBuffer()

        def isset(x):
            return not (x is None)

        if isset(self.format):
            term = [TERMINALS[self.format]]
        else:
            term = 'x11'

        ps_like = self.format in ['eps', 'pdf', 'ps']

        if isset(self.monochrome) and ps_like:
            term += ['monochrome' if self.monochrome else 'color']

        if isset(self.enhanced_text):
            term += ['enhanced' if self.enhanced_text else 'noenhanced']

        if isset(self.dashed_lines) and ps_like:
            term += ['dashed' if self.dashed_lines else 'solid']
        if isset(self.font) and ps_like:
            term += ['font "%s%s"' %
                     (self.font,
                      (",%s" % self.font_size) if isset(self.font_size)
                      else "")
                     ]

        if isset(self.rounded_caps):
            term += ['rounded' if self.rounded_caps else 'butt']

        if isset(self.size):
            term += ['size %s,%s' % (self.size)]

        if isset(self.font_size) and self.format == 'png':
            term += [self.font_size]

        g("set terminal %s" % " ".join(term))
        if isset(self.output):
            g("set out '/dev/null'")

        if self.xrange:
            g("set xrange [%s:%s]" % self.xrange)
        if self.xticks:
            g("set xtics %s, %s" % self.xticks)
        if self.xlabel:
            g("set xlabel '%s'" % self.xlabel)

        if self.yrange:
            g("set yrange [%s:%s]" % self.yrange)
        if self.yticks:
            g("set ytics %s, %s" % self.yticks)
        if self.ylabel:
            g("set ylabel '%s'" % self.ylabel)

        if self.key:
            g('set key %s' % self.key)
        if self.title:
            g('set title "%s"' % self.title)

        logscale = ""
        if self.xlog:
            logscale += "x"
        if self.ylog:
            logscale += "y"
        if logscale:
            g("set logscale %s" % logscale)

        for ls in self.line_styles:
            g("set style line %d %s" % (ls[0], ls[1]))

        plots = [c.gnuplot_cmd(self.default_style) for c in self.curves]
        if plots:
            g("plot " + ", ".join(plots))

        for l in self.labels:
            g("set label " + l.gnuplot_cmd())

        if isset(self.output):
            g('set out "%s"' % self.output)

        g("replot")

        if isset(self.output):
            g("set out") # close file

        return g


    def gnuplot_exec(self):
        pipe2gnuplot(self.gnuplot_commands())

    def gnuplot_save(self, fname):
        f = open(fname, 'w')
        f.write(str(self.gnuplot_commands()))
        f.close()

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,
                labels=[],
                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 == 'ps':
        terminal = 'postscript'
        if term_opts is None:
            term_opts = 'color enhanced'
    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:
        if isinstance(xticks, str):
            g('set xtics {0}'.format(xticks))
        else:
            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(str(gr))
        elif type(gr) == FileGraph:
            # formatter object
            plot.append(gr.gnuplot_cmd(style))
        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) == 6:
            par = (gr[0], gr[1], gr[2], gr[3], gr[4], style, gr[5])
            plot += ["'%s' using %s:%s:%s:%s with %s title '%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))
    for (x, y, txt) in labels:
        g('set label "%s" at %f,%f center' % (txt, x, y))
    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 make_cmd(self, graphs):
        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 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 self.make_cmd(graphs)

    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)

    def do_linedemo(self, _):
        graphs = ["%d title 'ls %d'" % (x, x) #, x) #  with linespoints ls %d
                  for x in xrange(1,10)]
        graphs += ["%d title 'lt %d' with lines lt %d" % (10 + x, x, x)
                   for x in xrange(1,10)]
        self.options.yrange = (0, 20)
        self.options.xrange = (0, 10)
        self.options.xticks = (0, 10)
        self.options.yticks = (0, 1)
        self.options.title = "%s Default Line Styles" % self.options.format.upper()
        self.options.ylabel = "Styles"
        self.options.term_opts = "" # avoid default

        cmd = self.make_cmd(graphs)
        pipe2gnuplot(cmd)


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