-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatapair.py
486 lines (388 loc) · 15.7 KB
/
datapair.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
from artist import Artist
from line import Line
from axis import Axis
from color import Color
from marker import *
def subsetOfData(data, region, start, end):
"""
Given a set of data points, this function returns the data points
that are between start and end.
More specifically, start and end correspond to range limits in region.
For every entry in range that is between start and end, the corresponding entry
in data is returned in a list.
"""
subset = []
for i in range(len(region)):
if region[i] >= start and region[i] <= end:
subset.append(data[i])
return subset
class DataPair(object):
"""
Represents a 2-D set of data. Contains the X and Y data, pointers to the
x and y axes, and maintains the lines and markers that are drawn.
"""
def __init__(self, canvas, x, y, formatString='', plot=None, xaxis=None, yaxis=None, linesVisible=True, markersVisible=True, lineProps={}, markerProps={}):
"""
**Constructor**
x, y
lists of something that can be plotted. Normally these are numbers, but
they can be anything that Axis.mapDataToPlot(), min(), and max() can
interpret.
formatString
a string that specifies some simple line and marker properties. An example is
'ro' for circle markers, and markers and lines that are red. This is applied
before the lineProps and markerProps.
xaxis, yaxis
the Axis instances that this data will be drawn in reference to. Usually
xaxis is the abscissa and yaxis is the ordinate, but they can be reversed.
linesVisible, markersVisible
Specify whether the lines and markers should be visible universally for this
datapair. Individual lines or markers can be hidden by updating those
individual objects. These attributes are used to be more efficient if some
things are not going to be drawn. If the lines or markers are changed from
hidden to visible, the DataPair.makeLinesAndMarkers should be called before
calling DataPair.draw or else they may not appear.
lineProps, markerProps
Properties for the line segments and markers that are drawn.
"""
self._canvas = canvas
self._lineProps = {}
self._markerProps = {}
self._linesVisible = linesVisible
self._markersVisible = markersVisible
self._markerClass = CircleMarker
self.setX(x)
self.setY(y)
self.setXAxis(xaxis)
self.setYAxis(yaxis)
self.setPlot(plot)
self.setFormatString(formatString)
self.setLineProps(**lineProps)
self.setMarkerProps(**markerProps)
def canvas(self):
return self._canvas
def setX(self, x):
"""Set the x data."""
if isinstance(x, list):
self._x = x
def setY(self, y):
"""Set the y data."""
if isinstance(y, list):
self._y = y
def setXAxis(self, xaxis):
"""Set the x axis."""
if isinstance(xaxis, Axis):
self._xaxis = xaxis
else:
self._xaxis = None
def setYAxis(self, yaxis):
"""Set the y axis."""
if isinstance(yaxis, Axis):
self._yaxis = yaxis
else:
self._yaxis = None
def setPlot(self, plot):
"""Set the plot this DataPair is attached to."""
# TODO need to fix this
#if isinstance(plot, Plot):
if True:
self._plot = plot
else:
self._plot = None
def plot(self):
return self._plot
def setFormatString(self, f):
"""
Set the format string.
The string should consist of three characters, clm. c specifies the
color, l specifies the line, and m specifies the marker. A color
must be specified, but if the line or marker should be hidden, a space
can be used in its place. A string that is not exactly three characters
long will not be processed (and so the defaults will be used instead).
If a string has an invalid character, then the default is used.
The following characters are valid:
========= =====
Character Color
========= =====
r red
g green
b blue
k black
w white
========= =====
========= =====
Character Line Style
========= =====
\- solid
= dashed
! dash-dot
: dotted
========= =====
========= =====
Character Marker
========= =====
o circle
s square
\| vertical
\- horizontal
\+ plus
x x
\* star
^ uptriangle
v downtriangle
< lefttriangle
> righttriangle
========= =====
"""
if len(f) != 3:
return
colors = {'r': Color('red'),
'g': Color('green'),
'b': Color('blue'),
'k': Color('black'),
'w': Color('white'),
}
lines = {'-': 'solid',
'=': 'dash',
'!': 'dashdot',
':': 'dot',
}
colorChar = f[0]
lineChar = f[1]
markerChar = f[2]
lineProps = {}
markerProps = {}
if lineChar == ' ':
self.setLinesVisible(False)
if markerChar == ' ':
self.setMarkersVisible(False)
if colorChar in colors.keys():
lineProps['color'] = colors[colorChar]
markerProps['color'] = colors[colorChar]
markerProps['fillcolor'] = colors[colorChar]
if lineChar in lines.keys():
lineProps['style'] = lines[lineChar]
self.setMarkerType(markerChar)
self.setLineProps(**lineProps)
self.setMarkerProps(**markerProps)
def setMarkerType(self, m='o'):
"""
Set the marker type. This accepts any single character
string accepted as a marker for the format string, or
the following strings:
none
circle
square
vertical line
horizontal line
plus
x
star
uptriangle
downtriangle
lefttriangle
righttriangle
"""
markers = {'o': CircleMarker,
'circle': CircleMarker,
's': SquareMarker,
'square': SquareMarker,
'|': VerticalMarker,
'vertical': VerticalMarker,
'-': HorizontalMarker,
'_': HorizontalMarker,
'horizontal': HorizontalMarker,
'+': PlusMarker,
'plus': PlusMarker,
'x': XMarker,
'*': StarMarker,
'star': StarMarker,
'^': UpTriangleMarker,
'uptriangle': UpTriangleMarker,
'v': DownTriangleMarker,
'downtriangle': DownTriangleMarker,
'<': LeftTriangleMarker,
'lefttriangle': LeftTriangleMarker,
'>': RightTriangleMarker,
'righttriangle': RightTriangleMarker,
}
if m in markers.keys():
self._markerClass = markers[m]
else:
self._markerClass = None
def setLinesVisible(self, v=True):
"""Set whether the lines are visible universally."""
if isinstance(v, bool):
self._linesVisible = v
def setMarkersVisible(self, v=True):
"""Set whether the markers are visible universally."""
if isinstance(v, bool):
self._markersVisible = v
def setLineProps(self, **kwprops):
"""Set the line arguments."""
self._lineProps.update(kwprops)
def setMarkerProps(self, **kwprops):
"""
Set the marker arguments.
In addition to accepting marker arguments, this also accepts
a keyword 'marker' which, if specified, will call setMarkerType
with the value.
"""
if 'marker' in kwprops.keys():
self.setMarkerType(kwprops.pop('marker'))
self._markerProps.update(kwprops)
def linesVisible(self):
"""Return whether the lines are universally visible."""
return self._linesVisible
def markersVisible(self):
"""Return whether the markers are universally visible."""
return self._markersVisible
def xAxis(self):
"""Return the x Axis instance."""
return self._xaxis
def yAxis(self):
"""Return the y Axis instance."""
return self._yaxis
def maxXValue(self, inSubRegion=False):
"""
Get the maximum value in the x data.
If inSubRegion is False, then this finds the appropriate value
in the entire data set.
If inSubRegion is True, then it determines the region on the other
axis that will be displayed, and finds the appropriate value within
that region. If the other axis is autoscaled, then this defaults to
inSubRegion=False behavior.
If the other axis is not currently defined, then this defaults to
inSubRegion=False behavior.
"""
if inSubRegion and self.yAxis() is not None and not self.yAxis().autoscaled():
return max(subsetOfData(self._x, self._y, *self.yAxis().dataRange()))
return max(self._x)
def maxYValue(self, inSubRegion=False):
"""
Get the maximum value in the y data.
If inSubRegion is False, then this finds the appropriate value
in the entire data set.
If inSubRegion is True, then it determines the region on the other
axis that will be displayed, and finds the appropriate value within
that region. If the other axis is autoscaled, then this defaults to
inSubRegion=False behavior.
If the other axis is not currently defined, then this defaults to
inSubRegion=False behavior.
"""
if inSubRegion and self.xAxis() is not None and not self.xAxis().autoscaled():
return max(subsetOfData(self._y, self._x, *self.xAxis().dataRange()))
return max(self._y)
def minXValue(self, inSubRegion=False):
"""
Get the minimum value in the x data.
If inSubRegion is False, then this finds the appropriate value
in the entire data set.
If inSubRegion is True, then it determines the region on the other
axis that will be displayed, and finds the appropriate value within
that region. If the other axis is autoscaled, then this defaults to
inSubRegion=False behavior.
If the other axis is not currently defined, then this defaults to
inSubRegion=False behavior.
"""
if inSubRegion and self.yAxis() is not None and not self.yAxis().autoscaled():
return min(subsetOfData(self._x, self._y, *self.yAxis().dataRange()))
return min(self._x)
def minYValue(self, inSubRegion=False):
"""
Get the minimum value in the y data.
If inSubRegion is False, then this finds the appropriate value
in the entire data set.
If inSubRegion is True, then it determines the region on the other
axis that will be displayed, and finds the appropriate value within
that region. If the other axis is autoscaled, then this defaults to
inSubRegion=False behavior.
If the other axis is not currently defined, then this defaults to
inSubRegion=False behavior.
"""
if inSubRegion and self.xAxis() is not None and not self.xAxis().autoscaled():
return min(subsetOfData(self._y, self._x, *self.xAxis().dataRange()))
return min(self._y)
def makeLinesAndMarkers(self):
"""
Create the Lines and Markers that will be drawn on the Figure.
The Lines and Markers will only be created if they are currently
set to be visible in this DataPair.
"""
(ox, oy, w, h) = self.plot().axesRegion()
minX = self.xAxis().position()[0]
maxX = self.xAxis().end()[0]
minY = self.yAxis().position()[1]
maxY = self.yAxis().end()[1]
xPlotCoords = map(lambda value: self._xaxis.mapDataToPlot(value), self._x)
yPlotCoords = map(lambda value: self._yaxis.mapDataToPlot(value), self._y)
self._lineSegments = []
self._markers = []
# Make the line segments
if self.linesVisible():
for i in range(min(len(xPlotCoords), len(yPlotCoords)) - 1):
x1 = xPlotCoords[i]
x2 = xPlotCoords[i+1]
y1 = yPlotCoords[i]
y2 = yPlotCoords[i+1]
# Do not bother making this line because it is entirely
# outside the plot's view. This does not get all the possible
# lines, but will get some fraction of them. The rest will
# be clipped by the canvas, so we don't really have to worry
# about them. This is primarily useful for instances like when
# x=range(0, 1000) but the plot is only showing (0, 10).
if (x1 < minX and x2 < minX) or (x1 > maxX and x2 > maxX) \
or (y1 < minY and y2 < minY) or (y1 > maxY and y2 > maxY):
continue
line = Line(self.canvas(), **self._lineProps)
line.setPoints(x1,
y1,
x2,
y2,
ox,
oy)
line.setClipPath(self.plot().axesRegion())
self._lineSegments.append(line)
# Make the markers
if self.markersVisible() and self._markerClass is not None:
for x, y in zip(xPlotCoords, yPlotCoords):
# Do not bother making this marker because it is outside
# the plot's view
if (x < minX or x > maxX) or (y < minY or y > maxY):
continue
marker = self._markerClass(self.canvas(), **self._markerProps)
marker.setOrigin(ox, oy)
marker.setPosition(x, y)
self._markers.append(marker)
def clear(self):
self.remove()
def remove(self):
"""
Remove all the Lines and Markers from the Figure.
"""
try:
for line in self._lineSegments:
line.remove()
for marker in self._markers:
marker.remove()
except:
# self._lineSegments or self._markers probably doesn't exist yet
pass
def draw(self, *args, **kwargs):
"""
Draw the Lines and Markers to the Figure.
Note: This does not actually override Artist.draw() because DataPair does
not subclass Artist.
"""
# Need to save the current lineSegments and markers so that they can be removed from
# the plot when the plot is next drawn
# Draw lines before markers so that the markers cover the lines when the overlap
# on the canvas.
if self.linesVisible():
for line in self._lineSegments:
line.draw()
if self.markersVisible():
for marker in self._markers:
marker.draw()
self._oldLineSegments = self._lineSegments
self._oldMarkers = self._markers