Skip to content

Commit 88a8c2e

Browse files
committed
Fixed recalculation of interquartile ranges
1 parent 8044e9d commit 88a8c2e

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

examples/statistics/customized_violin_demo.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
import numpy as np
1919

2020

21-
def adjacent_values(vals):
22-
q1, q3 = np.percentile(vals, [25, 75])
23-
inter_quartile_range = q3 - q1
24-
25-
upper_adjacent_value = q3 + inter_quartile_range * 1.5
21+
def adjacent_values(vals, q1, q3):
22+
upper_adjacent_value = q3 + (q3 - q1) * 1.5
2623
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])
2724

28-
lower_adjacent_value = q1 - inter_quartile_range * 1.5
25+
lower_adjacent_value = q1 - (q3 - q1) * 1.5
2926
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
3027
return [lower_adjacent_value, upper_adjacent_value]
3128

@@ -62,10 +59,11 @@ def set_axis_style(ax, labels):
6259
pc.set_edgecolor('black')
6360
pc.set_alpha(1)
6461

65-
quartiles = (np.percentile(data, [25, 50, 75], axis=1))
66-
medians = quartiles[1]
67-
inter_quartile_ranges = quartiles[[0, 2]].T
68-
whiskers = [adjacent_values(sorted_array) for sorted_array in data]
62+
quartile1, medians, quartile3 = np.percentile(data, [25, 50, 75], axis=1)
63+
inter_quartile_ranges = np.vstack([quartile1, quartile3]).T
64+
whiskers = [
65+
adjacent_values(sorted_array, q1, q3)
66+
for sorted_array, q1, q3 in zip(data, quartile1, quartile3)]
6967

7068
# plot whiskers as thin lines, quartiles as fat lines,
7169
# and medians as points

0 commit comments

Comments
 (0)