Note
Go to the endto download the full example code.
Box plot vs. violin plot comparison#
Note that although violin plots are closely related to Tukey's (1977)box plots, they add useful information such as the distribution of thesample data (density trace).
By default, box plots show data points outside 1.5 * the inter-quartilerange as outliers above or below the whiskers whereas violin plots showthe whole range of the data.
A good general reference on boxplots and their history can be foundhere:http://vita.had.co.nz/papers/boxplots.pdf
Violin plots require matplotlib >= 1.4.
For more information on violin plots, the scikit-learn docs have a greatsection:https://scikit-learn.org/stable/modules/density.html
importmatplotlib.pyplotaspltimportnumpyasnpfig,axs=plt.subplots(nrows=1,ncols=2,figsize=(9,4))# Fixing random state for reproducibilitynp.random.seed(19680801)# generate some random test dataall_data=[np.random.normal(0,std,100)forstdinrange(6,10)]# plot violin plotaxs[0].violinplot(all_data,showmeans=False,showmedians=True)axs[0].set_title('Violin plot')# plot box plotaxs[1].boxplot(all_data)axs[1].set_title('Box plot')# adding horizontal grid linesforaxinaxs:ax.yaxis.grid(True)ax.set_xticks([y+1foryinrange(len(all_data))],labels=['x1','x2','x3','x4'])ax.set_xlabel('Four separate samples')ax.set_ylabel('Observed values')plt.show()

Tags:plot-type: violinplot-type: boxplotdomain: statistics
References
The use of the following functions, methods, classes and modules is shownin this example: