20
20
21
21
def adjacent_values (vals ):
22
22
q1 , q3 = np .percentile (vals , [25 , 75 ])
23
- # inter-quartile range iqr
24
- iqr = q3 - q1
25
- # upper adjacent values
26
- uav = q3 + iqr * 1.5
27
- uav = np .clip (uav , q3 , vals [- 1 ])
28
- # lower adjacent values
29
- lav = q1 - iqr * 1.5
30
- lav = np .clip (lav , vals [0 ], q1 )
31
- return [lav , uav ]
23
+ inter_quartile_range = q3 - q1
24
+
25
+ upper_adjacent_value = q3 + inter_quartile_range * 1.5
26
+ upper_adjacent_value = np .clip (upper_adjacent_value , q3 , vals [- 1 ])
27
+
28
+ lower_adjacent_value = q1 - inter_quartile_range * 1.5
29
+ lower_adjacent_value = np .clip (lower_adjacent_value , vals [0 ], q1 )
30
+ return [lower_adjacent_value , upper_adjacent_value ]
32
31
33
32
34
33
def set_axis_style (ax , labels ):
@@ -42,19 +41,19 @@ def set_axis_style(ax, labels):
42
41
43
42
# create test data
44
43
np .random .seed (123 )
45
- dat = [sorted (np .random .normal (0 , std , 100 )) for std in range (1 , 5 )]
44
+ data = [sorted (np .random .normal (0 , std , 100 )) for std in range (1 , 5 )]
46
45
47
46
fig , (ax1 , ax2 ) = plt .subplots (nrows = 1 , ncols = 2 , figsize = (9 , 4 ), sharey = True )
48
47
49
48
# plot the default violin
50
49
ax1 .set_title ('Default violin plot' )
51
50
ax1 .set_ylabel ('Observed values' )
52
- ax1 .violinplot (dat )
51
+ ax1 .violinplot (data )
53
52
54
53
# customized violin
55
54
ax2 .set_title ('Customized violin plot' )
56
55
parts = ax2 .violinplot (
57
- dat , showmeans = False , showmedians = False ,
56
+ data , showmeans = False , showmedians = False ,
58
57
showextrema = False )
59
58
60
59
# customize colors
@@ -63,21 +62,17 @@ def set_axis_style(ax, labels):
63
62
pc .set_edgecolor ('black' )
64
63
pc .set_alpha (1 )
65
64
66
- # medians
67
- med = [np .percentile (sarr , 50 ) for sarr in dat ]
68
- # inter-quartile ranges
69
- iqr = [[np .percentile (sarr , 25 ), np .percentile (sarr , 75 )] for sarr in dat ]
70
- # upper and lower adjacent values
71
- avs = [adjacent_values (sarr ) for sarr in dat ]
65
+ medians = np .percentile (data , 50 , axis = 1 )
66
+ inter_quartile_ranges = list (zip (* (np .percentile (data , [25 , 75 ], axis = 1 ))))
67
+ whiskers = [adjacent_values (sorted_array ) for sorted_array in data ]
72
68
73
69
# plot whiskers as thin lines, quartiles as fat lines,
74
70
# and medians as points
75
- for i , median in enumerate (med ):
76
- # whiskers
77
- ax2 .plot ([i + 1 , i + 1 ], avs [i ], '-' , color = 'black' , linewidth = 1 )
78
- # quartiles
79
- ax2 .plot ([i + 1 , i + 1 ], iqr [i ], '-' , color = 'black' , linewidth = 5 )
80
- # medians
71
+ for i , median in enumerate (medians ):
72
+ ax2 .plot ([i + 1 , i + 1 ], whiskers [i ], '-' , color = 'black' , linewidth = 1 )
73
+ ax2 .plot (
74
+ [i + 1 , i + 1 ], inter_quartile_ranges [i ], '-' , color = 'black' ,
75
+ linewidth = 5 )
81
76
ax2 .plot (
82
77
i + 1 , median , 'o' , color = 'white' ,
83
78
markersize = 6 , markeredgecolor = 'none' )
0 commit comments