From 17a13f38ec449ed03dc12aff415ebdf25c37a56f Mon Sep 17 00:00:00 2001 From: Jan Richter Date: Sun, 14 Jun 2015 19:59:18 +0200 Subject: [PATCH] fixed stdev mode of Box plot for inputs of size 1 --- pygal/graph/box.py | 6 ++++-- pygal/test/test_box.py | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pygal/graph/box.py b/pygal/graph/box.py index 1469bee..4d97c43 100644 --- a/pygal/graph/box.py +++ b/pygal/graph/box.py @@ -55,6 +55,7 @@ class Box(Graph): return 'Min: %s Lower Whisker: %s Q1: %s Q2: %s Q3: %s '\ 'Upper Whisker: %s Max: %s' % tuple(map(sup, x)) else: + # 1.5IQR mode return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[2:5])) else: return sup(x) @@ -240,6 +241,8 @@ class Box(Graph): n = len(s) if not n: 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: q2 = median(s) # See 'Method 3' in http://en.wikipedia.org/wiki/Quartile @@ -277,7 +280,6 @@ class Box(Graph): elif mode == 'stdev': # one standard deviation above and below the mean of the data sd = stdev(s) - print s, sd b0 = bisect_left(s, q2 - sd) b4 = bisect_right(s, q2 + sd) q0 = s[b0] @@ -287,13 +289,13 @@ class Box(Graph): # one population standard deviation above and below # the mean of the data sdp = pstdev(s) - print s, sd b0 = bisect_left(s, q2 - sdp) b4 = bisect_right(s, q2 + sdp) q0 = s[b0] q4 = s[b4-1] outliers = s[:b0] + s[b4:] else: + # 1.5IQR mode q0 = q1 - 1.5 * iqr q4 = q3 + 1.5 * iqr return (min_s, q0, q1, q2, q3, q4, max_s), outliers diff --git a/pygal/test/test_box.py b/pygal/test/test_box.py index a3f98f7..2a277a8 100644 --- a/pygal/test/test_box.py +++ b/pygal/test/test_box.py @@ -138,6 +138,12 @@ def test_quartiles_stdev(): assert q0 >= q2 - SD 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(): box = ghostedBox() box.add('test1', [-1, 2, 3, 3.1, 3.2, 4, 5])