English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Violin Plot

Violin plots are similar to box plots, except that they also show the probability density of different values. These plots include a marker for the median of the data and a box representing the interquartile range, as shown in the standard box plot. The kernel density estimate is overlaid on the box plot. Like box plots, violin plots are used to compare the variable distributions (or sample distributions) across different 'categories'.

Violin plots are more informative than common plots. In fact, while box plots only show the mean/Summary statistics such as median and interquartile range, but violin plots show the complete distribution of the data.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
 import matplotlib.pyplot as plt
 np.random.seed(10)
 collectn_1 = np.random.normal(100, 10, 200)
 collectn_2 = np.random.normal(80, 30, 200)
 collectn_3 = np.random.normal(90, 20, 200)
 collectn_4 = np.random.normal(70, 25, 200)
 ## Merge these different collections into a list
 data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]
 # Create a figure instance
 fig = plt.figure()
 # Create axis instance
 ax = fig.add_axes([0,0,1,1])
 # Create box plot
 bp = ax.violinplot(data_to_plot)
 plt.show()

Execute the above example code to get the following results: