aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjörn B. Brandenburg <bbb@cs.unc.edu>2010-11-24 14:58:58 -0500
committerBjörn B. Brandenburg <bbb@cs.unc.edu>2010-11-24 14:58:58 -0500
commit0bd8dea3a724fcb2d4ffa3435f00fe45529e7868 (patch)
tree3f55fb6a431187fc27deb0c5ca2313c97a8ece93
parentdca4e3b844e759a029c52928c769c362ab5dce1d (diff)
support plotting histograms
-rwxr-xr-xgnuplot.py38
1 files changed, 36 insertions, 2 deletions
diff --git a/gnuplot.py b/gnuplot.py
index fae329d..a304809 100755
--- a/gnuplot.py
+++ b/gnuplot.py
@@ -86,14 +86,32 @@ class LiteralGraph(object):
86 (self.expr, title_txt, style_txt) 86 (self.expr, title_txt, style_txt)
87 87
88 88
89class HistogramGraph(object):
90 def __init__(self, fname, col=1, labels_col=None):
91 self.fname = fname
92 self.data_col = col
93 self.labels_col = labels_col
94
95 def __str__(self):
96 return self.gnuplot_cmd()
97
98 def gnuplot_cmd(self, default_style=None):
99 if self.labels_col != None:
100 lstr = ":xticlabels(%s)" % self.labels_col
101 else:
102 lstr = ""
103 return "'%s' using %s%s" % (self.fname, self.data_col, lstr)
104
105
89label = Label 106label = Label
90 107
91def curve(fname=None, literal=None, **kargs): 108def curve(fname=None, literal=None, histogram=None, **kargs):
92 if fname: 109 if fname:
93 return FileGraph(fname, **kargs) 110 return FileGraph(fname, **kargs)
94 elif literal: 111 elif literal:
95 return LiteralGraph(literal, **kargs) 112 return LiteralGraph(literal, **kargs)
96 113 elif histogram:
114 return HistogramGraph(histogram, **kargs)
97 115
98class Plot(object): 116class Plot(object):
99 def __init__(self): 117 def __init__(self):
@@ -119,14 +137,24 @@ class Plot(object):
119 self.ylog = None 137 self.ylog = None
120 self.ylabel = None 138 self.ylabel = None
121 139
140 self.boxwidth = None
141
122 self.key = None 142 self.key = None
123 self.title = None 143 self.title = None
124 144
125 self.labels = [] 145 self.labels = []
126 self.curves = [] 146 self.curves = []
147 self.style = {}
127 148
128 self.default_style = None # for plotted curves 149 self.default_style = None # for plotted curves
129 150
151 def setup_histogram(self, gap=None, boxwidth=0.9):
152 self.style['data'] = 'histogram'
153 if gap != None:
154 self.style['histogram'] = 'cluster gap %s' % gap
155 self.style['fill'] = 'solid 1.0 border -1'
156 self.boxwidth = '%.2f relative' % boxwidth
157
130 def gnuplot_commands(self, cmd_buf=None): 158 def gnuplot_commands(self, cmd_buf=None):
131 if cmd_buf: 159 if cmd_buf:
132 g = cmd_buf 160 g = cmd_buf
@@ -198,6 +226,12 @@ class Plot(object):
198 if logscale: 226 if logscale:
199 g("set logscale %s" % logscale) 227 g("set logscale %s" % logscale)
200 228
229 if self.boxwidth:
230 g('set boxwidth %s' % self.boxwidth)
231
232 for s in self.style:
233 g("set style %s %s" % (s, self.style[s]))
234
201 plots = [c.gnuplot_cmd(self.default_style) for c in self.curves] 235 plots = [c.gnuplot_cmd(self.default_style) for c in self.curves]
202 if plots: 236 if plots:
203 g("plot " + ", ".join(plots)) 237 g("plot " + ", ".join(plots))