1+ # Box plot - violin plot comparison
2+ #
3+ # Note that although violin plots are closely related to Tukey's (1977) box plots,
4+ # they add useful information such as the distribution of the sample data (density trace).
5+ #
6+ # By default, box plots show data points outside 1.5 x the inter-quartile range as outliers
7+ # above or below the whiskers wheras violin plots show the whole range of the data.
8+ #
9+ # Violin plots require matplotlib >= 1.4.
10+
11+ import matplotlib .pyplot as plt
12+ import numpy as np
13+
14+ fig , axes = plt .subplots (nrows = 1 ,ncols = 2 , figsize = (12 ,5 ))
15+
16+ # generate some random test data
17+ all_data = [np .random .normal (0 , std , 100 ) for std in range (6 , 10 )]
18+
19+ # plot violin plot
20+ axes [0 ].violinplot (all_data ,
21+ showmeans = False ,
22+ showmedians = True
23+ )
24+ axes [0 ].set_title ('violin plot' )
25+
26+ # plot box plot
27+ axes [1 ].boxplot (all_data )
28+ axes [1 ].set_title ('box plot' )
29+
30+ # adding horizontal grid lines
31+ for ax in axes :
32+ ax .yaxis .grid (True )
33+ ax .set_xticks ([y + 1 for y in range (len (all_data ))], )
34+ ax .set_xlabel ('xlabel' )
35+ ax .set_ylabel ('ylabel' )
36+
37+ # add x-tick labels
38+ plt .setp (axes , xticks = [y + 1 for y in range (len (all_data ))],
39+ xticklabels = ['x1' , 'x2' , 'x3' , 'x4' ],
40+ )
41+
42+ plt .show ()
0 commit comments