-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
42 lines (31 loc) · 1.06 KB
/
graph.py
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
from cStringIO import StringIO
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
class GraphToolException(Exception):
pass
def fig_to_data(fig):
s = StringIO()
fig.print_png(s)
r = s.getvalue()
s.close()
return r
class GraphTool(object):
def __init__(self):
pass
def commit_bar(self, _time, amount, _title='Unknown title',
_xlabel = 'Unknown time type', _ylabel='Unknown amount type'):
"""
Generate a bar plot with <time> on X, amount on Y.
Specify amount label with _ylabel, time label with _xlabel.
"""
fig = Figure(edgecolor='#FFFFFF', facecolor='#FFFFFF')
canvas = FigureCanvas(fig)
h = fig.add_subplot(111)
h.bar(_time, amount)
h.set_xlabel(_xlabel)
h.set_ylabel(_ylabel)
h.set_xbound(_time[0], _time[len(_time)-1])
h.set_ybound(0, int(max(amount)))
h.set_axis_bgcolor('#FFFFFF')
h.set_title(_title)
return fig_to_data(canvas)