-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlotInfo.py
330 lines (254 loc) · 10.3 KB
/
PlotInfo.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
import copy
import warnings
from LabelProperties import LabelProperties
class PlotInfo(object):
def __init__(self,
plotType,
xValues = None,
yValues = None,
xTickLabels = None,
yTickLabels = None,
xTickLabelPoints = None,
yTickLabelPoints = None,
xTickLabelProperties = None,
yTickLabelProperties = None,
label = None,
yMins = None,
yMaxes = None,
yErrors = None,
autosort = True,
xLimits = None,
errorBarColor = None,
errorBarLineWidth = None,
errorBarCapsize = None,
picker=None):
if xValues is None:
xValues = []
if yValues is None:
yValues = []
if xTickLabels is None:
xTickLabels = []
if yTickLabels is None:
yTickLabels = []
if xTickLabelPoints is None:
xTickLabelPoints = []
if yTickLabelPoints is None:
yTickLabelPoints = []
if yMins is None:
yMins = []
if yMaxes is None:
yMaxes = []
if yErrors is None:
yErrors = []
self.plotType = plotType
"""
Names the type of plot element
"""
self.xValues = xValues
"""
An array of x-axis values for the plot element
"""
self.yValues = yValues
"""
An array of y-axis values for the plot elements
"""
self.xTickLabels = xTickLabels
"""
A list of labels that should be drawn on the x-axis
"""
self.yTickLabels = yTickLabels
"""
A list of labels that should be drawn on the y-axis
"""
self.xTickLabelPoints = xTickLabelPoints
"""
The locations on the x-axis where the labels in :attr:`xTickLabels`
should be drawn
"""
self.yTickLabelPoints = yTickLabelPoints
"""
The locations on the x-axis where the labels in :attr:`xTickLabels`
should be drawn
"""
self._xTickLabelProperties = LabelProperties()
self._yTickLabelProperties = LabelProperties()
if xTickLabelProperties is not None:
self.xTickLabelProperties = xTickLabelProperties
if yTickLabelProperties is not None:
self.yTickLabelProperties = yTickLabelProperties
self.label = label
"""
A string used to label this plot element in a legend
"""
self.yMins = yMins
"""
For asymmetric error bars, a list of locations for the bottoms of this
plot element's error bars
"""
self.yMaxes = yMaxes
"""
For asymmetric error bars, a list of locations for the tops of this
plot element's error bars
"""
self.yErrors = yErrors
"""
For symmetric error bars, a list of error bar widths for this plot
element's error bars
"""
self.autosort = autosort
"""
If true, x/y pairs in :py:attr:`xValues` and :py:attr:`yValues` are
sorted by x value before the plot is rendered
"""
self.xLimits = xLimits
self.errorBarColor = errorBarColor
self.errorBarLineWidth = errorBarLineWidth
self.errorBarCapsize = errorBarCapsize
self.picker = picker
@property
def xTickLabelProperties(self):
"""
A dictionary of properties that control the appearance of the X axis'
tick labels. See :ref:`styling-labels` for more information on which
properties can be set.
"""
return self._xTickLabelProperties
@xTickLabelProperties.setter
def xTickLabelProperties(self, **props):
self._xTickLabelProperties.update(props)
@xTickLabelProperties.setter
def xTickLabelProperties(self, propsobj):
self._xTickLabelProperties.update(propsobj)
@property
def yTickLabelProperties(self):
"""
A dictionary of properties that control the appearance of the Y axis'
tick labels. See :ref:`styling-labels` for more information on which
properties can be set.
"""
return self._yTickLabelProperties
@yTickLabelProperties.setter
def yTickLabelProperties(self, props):
self._yTickLabelProperties.update(props)
def __str__(self):
return str(self.__dict__)
def _preDraw(self):
"""
The _preDraw function is called on all plot elements before drawing
occurs. This allows various fields to be set in a structured way
prior to actual drawing.
"""
pass
def setXTickLabelProperties(self, **propList):
# Deprecated: use xTickLabelProperties field instead
self._xTickLabelProperties.update(propList)
def setYTickLabelProperties(self, **propList):
# Deprecated: use yTickLabelProperties field instead
self._yTickLabelProperties.update(propList)
def split(self, pieces):
"""
Split this plot element into `pieces` separate plot elements, each of
which is responsible for a disjoint range of the original plot
element's x-axis. This is useful for situations where a plot element
cannot fit comfortably in a single plot.
"""
elements = []
numXVals = len(self.xValues)
valChunkSize = numXVals / pieces
valChunkRemainder = numXVals % pieces
for i in xrange(pieces):
element = copy.deepcopy(self)
if i < pieces - 1 or valChunkRemainder == 0:
element.xValues = self.xValues[i * valChunkSize:(i+1) * valChunkSize]
element.yValues = self.yValues[i * valChunkSize:(i+1) * valChunkSize]
else:
element.xValues = self.xValues[i * valChunkSize:]
element.yValues = self.yValues[i * valChunkSize:]
elements.append(element)
return elements
def getAttributes(self):
"""
Get the attributes dictionary used to style the plot element. Extending
this method allows plot elements to inherit attributes.
"""
kwdict = {}
if self.picker is not None:
kwdict['picker'] = self.picker
return kwdict
def draw(self, fig, axis, transform=None):
"""
The base method for drawing a plot element on the axis `axis` within
the figure `fig`. Other plot elements should override this method, but
should also be sure to call this method before doing any
element-specific processing.
"""
if len(self.xValues) > 0 and self.autosort:
# This is a total kludge --AR
sortAsymmetricErrorBars = len(self.yMins) > 0 or \
len(self.yMaxes) > 0
sortSymmetricErrorBars = len(self.yErrors) > 0
if sortAsymmetricErrorBars:
if len(self.yMins) != len(self.yValues):
warnings.warn("You don't have an error bar for every "
"point, some points will be truncated")
zipped = zip(self.xValues, self.yValues, self.yMins,
self.yMaxes)
zipped.sort()
self.xValues, self.yValues, self.yMins, self.yMaxes \
= zip(*zipped)
elif sortSymmetricErrorBars:
if len(self.yErrors) != len(self.yValues):
warnings.warn("You don't have an error bar for every "
"point, some points will be truncated")
zipped = zip(self.xValues, self.yValues, self.yErrors)
zipped.sort()
self.xValues, self.yValues, self.yErrors = zip(*zipped)
else:
zipped = zip(self.xValues, self.yValues)
zipped.sort()
self.xValues, self.yValues = zip(*zipped)
if len(self.xTickLabels) > 0:
if len(self.xTickLabelPoints) == 0:
axis.set_xticks(self.xValues[0:len(self.xTickLabels)])
else:
axis.set_xticks(self.xTickLabelPoints)
axis.set_xticklabels(self.xTickLabels, **self.xTickLabelProperties)
if len(self.yTickLabels) > 0:
if len(self.yTickLabelPoints) == 0:
axis.set_yticks(self.yValues[0:len(self.yTickLabels)])
else:
axis.set_yticks(self.yTickLabelPoints)
axis.set_yticklabels(self.yTickLabels, **self.yTickLabelProperties)
self.drawErrorBars(axis)
def drawErrorBars(self, axis, transform=None):
"""
Draw error bars for the plot element using the plot element's
`yMins`/`yMaxes` or `yErrors`. If `yMins` or `yMaxes` have non-zero
length, they are used to draw error bars that can be
asymmetric. Otherwise, `yErrors` are used to draw symmetric error bars.
If some transform should be applied to the error bars, it is provided
by the caller with the `transform` parameter.
"""
errorBarKeywords = {}
if self.errorBarColor is not None:
errorBarKeywords["ecolor"] = self.errorBarColor
elif hasattr(self, "color"):
errorBarKeywords["ecolor"] = self.color
if self.errorBarCapsize is not None:
errorBarKeywords["capsize"] = self.errorBarCapsize
if self.errorBarLineWidth is not None:
errorBarKeywords["linewidth"] = self.errorBarLineWidth
if hasattr(self, "lineStyle"):
errorBarKeywords["linestyle"] = self.lineStyle
if transform:
errorBarKeywords['transform'] = transform + axis.transData
errorBarKeywords["fmt"] = None
if len(self.yMins) > 0 and len(self.yMaxes) > 0:
numYVals = len(self.yValues)
yMin = [self.yValues[i] - self.yMins[i] for i in xrange(numYVals)]
yMax = [self.yMaxes[i] - self.yValues[i] for i in xrange(numYVals)]
errorBarKeywords["yerr"] = [yMin, yMax]
axis.errorbar(self.xValues, self.yValues, **errorBarKeywords)
elif len(self.yErrors) > 0:
errorBarKeywords["yerr"] = self.yErrors
axis.errorbar(self.xValues, self.yValues, **errorBarKeywords)