aboutsummaryrefslogtreecommitdiffstats
path: root/gnuplot.py
blob: 2604cb044c4d2e9955e00d4c2eb17c7609628f6c (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
#!/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']

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 FileGraph(object):
    def __init__(self, fname, xcol=1, ycol=2, error=None,
                 title=None,
                 style=None):
        self.fname = fname
        self.xcol  = xcol
        self.ycol  = ycol
        self.error = error
        self.title = title
        self.style = style

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

    def get_command(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 ""
        return "'%s' using %s%s%s" % \
            (self.fname, using_txt, title_txt, style_txt)

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 == '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:
        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.get_command(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))
    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()