-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotter.py
166 lines (127 loc) · 4 KB
/
plotter.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
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
from figure import *
from plot import *
from datapair import *
class FigureManager(object):
"""
A class that contains all the figures that have been created using the plotter
interface. It also keeps track of the active figure.
"""
_figures = [] # list of all figures that have been created
_active = None # index of the active figure
@staticmethod
def getActive():
"""
Return the active Figure object, or None if none exists.
"""
try:
return FigureManager._figures[FigureManager._active]
except TypeError, IndexError:
# _active is None or not in 0..len(_figures)
return None
@staticmethod
def setActive(fig):
"""
Check if fig already exists. If it does, then set it as active. If not,
then add it to the figures and set it as active.
"""
if isinstance(fig, Figure):
try:
FigureManager._active = FigureManager._figures.index(fig)
except ValueError:
# fig is not in _figures
FigureManager._figures.append(fig)
FigureManager._active = len(FigureManager._figures) - 1
def figure(width=600, height=400):
"""
Create a figure.
"""
fig = Figure(width, height)
FigureManager.setActive(fig)
return fig
def plot(*args, **kwargs):
"""
args are x1, y1, x2, y2, etc
Valid kwargs are:
new
Boolean specifying whether a new plot should be created on the current figure.
Defaults to False.
position
A 3-tuple specifying the location of this plot, of the form (numRows, numCols, numPlot).
Uses the current figure, if one exists, otherwise creates a new figure.
If the current figure has a current plot, then uses it, otherwise creates a
plot in position 1, 1, 1.
Running plot() multiple times will add more data to the last defined plot.
Running show() multiple times will show the last defined plot.
"""
newPlot = False
if 'new' in kwargs.keys():
newPlot = True
fig = FigureManager.getActive()
if fig is None:
figure()
fig = FigureManager.getActive()
plot = fig.getCurrentPlot()
if plot is None or newPlot is True:
plot = CartesianPlot(fig, fig.canvas())
fig.addPlot(plot)
position = [1, 1, 1]
if 'position' in kwargs.keys():
position = kwargs['position']
plot.setPlotLocation(*position)
args = list(args)
while len(args) > 0:
x = args.pop(0)
y = args.pop(0)
if len(args) > 0 and isinstance(args[0], str):
fs = args.pop(0)
else:
fs = ''
d = DataPair(fig.canvas(), x, y, fs)
plot.addDataPair(d)
return plot
def clearFigure():
"""
Clear the active figure and remove all plots from it.
"""
fig = FigureManager.getActive()
if fig is not None:
fig.clear()
fig.deleteAllPlots()
def show():
"""
Redraw the current figure and display it.
"""
fig = FigureManager.getActive()
if fig is not None:
# Every time the figure is shown, need to reinitialize it so that the sizes
# are all set correctly.
fig.canvas().__init__(fig, fig.canvas().scene().width(), fig.canvas().scene().height())
fig.draw()
_show_Qt()
def save(filename):
"""
Save the active figure to filename.
"""
fig = FigureManager.getActive()
if fig is not None:
fig.save(filename)
def listFonts():
"""
List all the available fonts for the canvas that is currently
in use.
"""
# TODO can change this so that it doesn't require instantiating a Figure
figure = Figure(600, 400)
figure.canvas().listFonts()
# import necessary modules
import sys
from PySide.QtCore import *
from PySide.QtGui import *
def _start_Qt():
try:
QApplication(sys.argv)
except RuntimeError:
pass
def _show_Qt():
qApp.exec_()
app = QApplication(sys.argv)