top of page
Search

Violin Plot in Python using Seaborn Library

Have you ever wondered a chart that can visualize the data distribution and also give the statistical information of the data.

A Violin plot or Violin chart is a statistical graphic plot that compares the probability distribution that displays the entire data distribution along with statistics of data distribution.

Violin chart is an effective data visualization technique that combines elements of a box plot and a kernel density plot. As, a quick reference a box plot or the whisker plot is a type of chart that depicts numerical data by their quartiles and visualizes the data shape. One can derive minimum, maximum, first or lower quartile, median, upper quartile or third quartile. One can identify outliers as well from a box plot.

Kernel density plot is a type of plot that displays the distribution of values in a dataset using one continuous curve, similar to histogram but better than it as in histogram the data shape distribution is affected by the number of bins used.

Violin chart is an Categorical distribution plot, has many similarities with boxplot.

Features of Violin chart:

As mentioned earlier, this statistical graph displays probability distributions across different categories or groups. It gives information on median of the data, a box or marker that indicates the interquartile range which is 25% to 75% of entire data distributed, a kernel density plot on each side showing distribution and probability density in form of a shape. The width of the violin plot at any given point represents the probable density or frequency of data point at that value.

Interpretation of a violin plot:

The outer shape of the violin plot shows the distribution or spread of the data values. A wider section means there are more data points clustered around those values, while a narrower section indicates fewer data points in that value range.

Inside the violin shape, you may see a smaller box-like structure. The line in the middle of this box represents the median value, which is the middle point of the data when arranged in order. The top and bottom of the box show the range where the middle 50% of the data lies.

You might also see thin lines (whiskers) extending from the box. These indicate the minimum and maximum data values, excluding any outliers or extreme values that are very different from the rest of the data.

So, by looking at the violin plot, you can quickly understand:

The overall distribution of the data values (from the outer shape)

Where most of the data is concentrated (wider sections)

The median value (line inside the box)

The range containing the middle 50% of data (box height)

The minimum and maximum values (whisker length)

The violin plot combines the simple summary of a box plot with the detailed distribution shape, giving you a complete picture of the data in a single visualization.

Â

Â

History:

This chart was first proposed by Jerry L.Hintze and Ray D.Nelson in 1997 to provide more information than box plots.

Use cases:

Some of the uses are listed below,

Â·Â Â Â Â Â Â Â Â  It helps in visualizing and comparing the distributions across different groups or categories. Say, one wants to know about the distribution of a numerical variable across different categories or groups like car prices across different manufacturers etc. as, we can compare them side by side, it is an effective visualization tool for Exploratory data analysis.

Â·Â Â Â Â Â Â Â Â  Multimodal or bimodal distributions can be detected, which is difficult to identify with boxplots.

Â·Â Â Â Â Â Â Â Â  Identification of outliers.

Â·Â Â Â Â Â Â Â Â  Supports EDA Â by providing a detailed visualization of the distribution shape, violin charts play a vital role in identifying underlying data patterns and relationships between variables.

Â Â

Â

Violin chart in Python-Seaborn library:

Below steps show in detail, how these are done

Install the seaborn followed by, importing matplotlib and seaborn

Â

We have downloaded existing dataset, cars and data preprocessing was done here.

Selected whitegrid for background

Data was organized and preprocessed, selected few columns for visualization purpose.

Lets see independent distribution

In the above plot, outer is KDE, inner is the box plot, where the white dot represents the median .

The above one shows the distribution of cylinders with engine displacement.

Another way to show the violin plot is given below,

Lets pass another variable in hue parameter, which is origin

Selecting only 2 origin countries,

Now lets see about how, symmetry can be effected.

Until now, the kde distribution on both ends belonged to one country, here the distribution has changed in this case one side belongs to Japan and other to Europe.

Here in this case, in the boxplot, quartiles are shown

Â

Â

Scaling of the plot can be altered,

Scaling by hue is discussed below,

After scaling by hue is done, the distribution for cylinder â€˜6â€™ has changed.

Styling options, here we can select which country to come first.

Another styling where KDE borders are defined, one can play around as per their visualization needs,

KDE can also be styled,

KDE can also be styled,

There are many other styling options available, but only few has been explored, color can be defined.

Shows the full distribution shape and probability density.

Allows comparison of distributions across multiple groups.

Can be harder to interpret for readers unfamiliar with violin plots.

May appear visually noisier or cluttered compared to box plots.

Conclusion:

Violin plots are valuable tools for visualizing data distributions and understanding the variability within the datasets. They offer advantages such as displaying probability density and handling unequal sample sizes, but they may also have limitations, such as potential misinterpretation and clutter for large datasets. Overall, violin plots are effective for exploratory data analysis and providing insights into the distributional characteristics of the data.

Â

References: