Browse Source

Merge pull request #133 from gjvnq/master

Added new mode to box plot chart: "extremes"
pull/136/head
Mounier Florian 11 years ago
parent
commit
750d76ddd5
  1. 4
      pygal/config.py
  2. 16
      pygal/graph/box.py
  3. 29
      pygal/test/test_box.py

4
pygal/config.py

@ -320,6 +320,10 @@ class Config(CommonConfig):
"ie: For hermite interpolation, you can set the cardinal tension with"
"{'type': 'cardinal', 'c': .5}", int)
mode = Key(
None, str, "Value", "Sets the mode to be used. (Currently only supported on box plot)",
"May be %s" % ' or '.join(["1.5IQR", "extremes"]))
order_min = Key(
None, int, "Value", "Minimum order of scale, defaults to None")

16
pygal/graph/box.py

@ -47,6 +47,9 @@ class Box(Graph):
def format_maybe_quartile(x):
if is_list_like(x):
if self.mode == "extremes":
return 'Min: %s Q1: %s Q2: %s Q3: %s Max: %s' % tuple(map(sup, x))
else:
return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[1:4]))
else:
return sup(x)
@ -58,7 +61,7 @@ class Box(Graph):
within the rendering process
"""
for serie in self.series:
serie.values = self._box_points(serie.values)
serie.values = self._box_points(serie.values, self.mode)
if self._min:
self._box.ymin = min(self._min, self.zero)
@ -160,10 +163,15 @@ class Box(Graph):
sum(quartiles) / len(quartiles)))
@staticmethod
def _box_points(values):
def _box_points(values, mode='1.5IQR'):
"""
Default mode: (mode='1.5IQR' or unset)
Return a 5-tuple of Q1 - 1.5 * IQR, Q1, Median, Q3,
and Q3 + 1.5 * IQR for a list of numeric values.
Extremes mode: (mode='extremes')
Return a 5-tuple of minimum, Q1, Median, Q3,
and maximum for a list of numeric values.
The iterator values may include None values.
@ -203,6 +211,10 @@ class Box(Graph):
q3 = 0.25 * s[3*m+1] + 0.75 * s[3*m+2]
iqr = q3 - q1
if mode == 'extremes':
q0 = min(s)
q4 = max(s)
else:
q0 = q1 - 1.5 * iqr
q4 = q3 + 1.5 * iqr
return q0, q1, q2, q3, q4

29
pygal/test/test_box.py

@ -49,6 +49,35 @@ def test_quartiles():
assert q3 == 4
assert q4 == 4
def test_quartiles_min_extremes():
a = [-2.0, 3.0, 4.0, 5.0, 8.0] # odd test data
q0, q1, q2, q3, q4 = Box._box_points(a, mode='extremes')
assert q1 == 7.0 / 4.0
assert q2 == 4.0
assert q3 == 23 / 4.0
assert q0 == -2.0 # min
assert q4 == 8.0 # max
b = [1.0, 4.0, 6.0, 8.0] # even test data
q0, q1, q2, q3, q4 = Box._box_points(b, mode='extremes')
assert q2 == 5.0
c = [2.0, None, 4.0, 6.0, None] # odd with None elements
q0, q1, q2, q3, q4 = Box._box_points(c, mode='extremes')
assert q2 == 4.0
d = [4]
q0, q1, q2, q3, q4 = Box._box_points(d, mode='extremes')
assert q0 == 4
assert q1 == 4
assert q2 == 4
assert q3 == 4
assert q4 == 4
def test_simple_box():
box = ghostedBox()

Loading…
Cancel
Save