-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot.py
More file actions
49 lines (40 loc) · 1.47 KB
/
plot.py
File metadata and controls
49 lines (40 loc) · 1.47 KB
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
# -*- coding: utf-8 -*-
""" plot
.. moduleauthor:: limseok <[email protected]>
"""
# import matplotlib
#
# matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
class Canvas(object):
def __init__(self, title='', xlab='x', ylab='y', xrange=None, yrange=None):
self.fig = plt.figure()
self.fig.set_facecolor('white')
self.ax = self.fig.add_subplot(111)
self.ax.set_title(title)
self.ax.set_xlabel(xlab)
self.ax.set_ylabel(ylab)
if xrange:
self.ax.set_xlim(xrange)
if yrange:
self.ax.set_ylim(yrange)
self.legend = []
@staticmethod
def show():
plt.show()
def save(self, filename='plot.png'):
FigureCanvasAgg(self.fig).print_png(open(filename, 'wb'))
def plot(self, data, color='green', style='-', width=2, legend=None, xrange=None):
if callable(data) and xrange:
x = [xrange[0] + 0.01 * i * (xrange[1] - xrange[0]) for i in range(0, 101)]
y = [data(p) for p in x]
else:
x, y = [p[0] for p in data], [p[1] for p in data]
q = self.ax.plot(x, y, linestyle=style, linewidth=width, color=color)
if legend:
self.legend.append((q[0], legend))
legend = self.ax.legend([e[0] for e in self.legend],
[e[1] for e in self.legend])
legend.get_frame().set_alpha(0.5)
return self