-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBar.py
120 lines (95 loc) · 3.35 KB
/
Bar.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
from matplotlib import pyplot
from PlotInfo import *
from boomslang.Label import Label
import warnings
class Bar(PlotInfo):
"""
A bar chart consisting of a single series of bars.
"""
def __init__(self,
label=None,
labelBars=False,
width=0.8,
linewidth=1.0,
color="black",
edgeColor=None,
hatch=None,
align="center",
**kwargs
):
super(Bar,self).__init__("bar", **kwargs)
self.label = label
self.labelBars = labelBars
self.width = width
"""
The width of each bar in the set of bars
"""
self.linewidth = linewidth
"""
The width of the line that borders each bar
"""
self.color = color
"""
The color of the bars
"""
self.edgeColor = edgeColor
"""
The color of the line that borders each bar
"""
self.hatch = hatch
"""
The style of hatching used within the bar, if any. Valid values are any
marker type, ``/``, ``//``, ``\`` and ``\\``
"""
self.align = align
"""
If `center` (default), `xValues` values denote the center of each
bar. If `edge`, `xValues` values denote the left edge of each bar.
"""
self.padding = 0
"""
Amount of padding between the edge of the left- and rightmost bars and
the bounding box for the plot
"""
def _preDraw(self):
self.xLimits = ((min(self.xValues) - self.width / 2.0) - self.padding,
(max(self.xValues) + self.width / 2.0) + self.padding)
def draw(self, fig, axis, transform=None):
super(Bar,self).draw(fig, axis)
return self._draw(fig, axis, transform)
def _draw(self, fig, axis, transform=None):
kwdict = self.getAttributes()
plotObjects = []
labels = [self.label]
if transform:
xValues = [transform.transform((x,0))[0] for x in self.xValues]
else:
xValues = self.xValues
plotObjects.append(axis.bar(xValues, self.yValues, **kwdict)[0])
if self.labelBars:
maxYValue = max(self.yValues)
for i in xrange(len(self.yValues)):
xVal = xValues[i]
yVal = self.yValues[i]
label = Label(xVal, yVal, "%d" % (yVal))
label.setTextOffset(0, 0.05 * maxYValue)
label.marker = 'x'
(labelObjects, labelLabels) = label.draw(fig, axis)
plotObjects.extend(labelObjects)
labels.extend(labelLabels)
return [plotObjects, labels]
def getAttributes(self):
kwdict = super(Bar,self).getAttributes()
kwdict["color"] = self.color
kwdict["label"] = self.label
kwdict["width"] = self.width
kwdict["linewidth"] = self.linewidth
kwdict["align"] = self.align
if self.hatch is not None:
kwdict["hatch"] = self.hatch
warnings.warn(
"Setting hatch for bar charts only seems to work when "
"exporting to svg, png, or pdf", Warning)
if self.edgeColor is not None:
kwdict["edgecolor"] = self.edgeColor
return kwdict