Browse Source

fixed stdev mode of Box plot for inputs of size 1

pull/226/head
Jan Richter 10 years ago
parent
commit
17a13f38ec
  1. 6
      pygal/graph/box.py
  2. 6
      pygal/test/test_box.py

6
pygal/graph/box.py

@ -55,6 +55,7 @@ class Box(Graph):
return 'Min: %s Lower Whisker: %s Q1: %s Q2: %s Q3: %s '\ return 'Min: %s Lower Whisker: %s Q1: %s Q2: %s Q3: %s '\
'Upper Whisker: %s Max: %s' % tuple(map(sup, x)) 'Upper Whisker: %s Max: %s' % tuple(map(sup, x))
else: else:
# 1.5IQR mode
return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[2:5])) return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[2:5]))
else: else:
return sup(x) return sup(x)
@ -240,6 +241,8 @@ class Box(Graph):
n = len(s) n = len(s)
if not n: if not n:
return (0, 0, 0, 0, 0, 0, 0), [] return (0, 0, 0, 0, 0, 0, 0), []
elif n == 1:
return (s[0], s[0], s[0], s[0], s[0], s[0], s[0]), []
else: else:
q2 = median(s) q2 = median(s)
# See 'Method 3' in http://en.wikipedia.org/wiki/Quartile # See 'Method 3' in http://en.wikipedia.org/wiki/Quartile
@ -277,7 +280,6 @@ class Box(Graph):
elif mode == 'stdev': elif mode == 'stdev':
# one standard deviation above and below the mean of the data # one standard deviation above and below the mean of the data
sd = stdev(s) sd = stdev(s)
print s, sd
b0 = bisect_left(s, q2 - sd) b0 = bisect_left(s, q2 - sd)
b4 = bisect_right(s, q2 + sd) b4 = bisect_right(s, q2 + sd)
q0 = s[b0] q0 = s[b0]
@ -287,13 +289,13 @@ class Box(Graph):
# one population standard deviation above and below # one population standard deviation above and below
# the mean of the data # the mean of the data
sdp = pstdev(s) sdp = pstdev(s)
print s, sd
b0 = bisect_left(s, q2 - sdp) b0 = bisect_left(s, q2 - sdp)
b4 = bisect_right(s, q2 + sdp) b4 = bisect_right(s, q2 + sdp)
q0 = s[b0] q0 = s[b0]
q4 = s[b4-1] q4 = s[b4-1]
outliers = s[:b0] + s[b4:] outliers = s[:b0] + s[b4:]
else: else:
# 1.5IQR mode
q0 = q1 - 1.5 * iqr q0 = q1 - 1.5 * iqr
q4 = q3 + 1.5 * iqr q4 = q3 + 1.5 * iqr
return (min_s, q0, q1, q2, q3, q4, max_s), outliers return (min_s, q0, q1, q2, q3, q4, max_s), outliers

6
pygal/test/test_box.py

@ -138,6 +138,12 @@ def test_quartiles_stdev():
assert q0 >= q2 - SD assert q0 >= q2 - SD
assert all(n in outliers for n in [6, 12, 16, 53, 54, 61, 75]) assert all(n in outliers for n in [6, 12, 16, 53, 54, 61, 75])
b = [5] # test for posible zero division
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
b, mode='stdev')
assert min_s == q0 == q1 == q2 == q3 == q4 == max_s == b[0]
assert outliers == []
def test_simple_box(): def test_simple_box():
box = ghostedBox() box = ghostedBox()
box.add('test1', [-1, 2, 3, 3.1, 3.2, 4, 5]) box.add('test1', [-1, 2, 3, 3.1, 3.2, 4, 5])

Loading…
Cancel
Save