# Violin Plots in Python – A Simple Guide

A violin plot is a cross between a box plot and a kernel density plot that displays data peaks. It’s used to show how numerical data is distributed. In contrast to a box plot, which can only provide summary statistics, violin plots show summary statistics and the density of each variable.

Violin plots are used to examine the distribution of numerical data, and they are especially effective for comparing distributions across various groups. The peaks, troughs, and tails of each group’s density curve may be compared to determine where groupings are similar and where they differ.

### Reasons why one should use Violin Plots?

1. Violin graphs are similar to box plots and density plots but superior!
2. The violin graph is aesthetically appealing.
3. Even if your data does not follow a normal distribution, violin graphs are completely acceptable. They are effective in visualizing both quantitative and qualitative data.
4. Reading the violin shape is similar to reading a density plot: the thicker part indicates that the values in that region of the violin have a greater frequency. In comparison, the thinner part indicates a lower frequency.

## Plotting Violin Plots in Python

Before jumping to the code implementation, let’s first look at the libraries in Python which can be used to implement Violin plots in Python.

### Libraries in Python that can be used for making violin plots

#### Seaborn

Seaborn is developed on top of Matplotlib, Python’s fundamental visualization toolkit. It is supposed to be a supplement, not a substitute. Seaborn, on the other hand, has several extremely crucial traits.

Read more about seaborn @ Python Seaborn Tutorial

#### Matplotlib

Matploitlib is a Python library that is used for plotting. It provides objected-oriented APIs for integrating plots into programs. It is a cross-platform toolkit for creating 2D charts from array data. It provides an object-oriented API for embedding charts in Python GUI toolkit-based applications.

Read more about seaborn @ Python Matplotlib Tutorial

#### Plotly

Python Plotly Toolkit is an open-source library that may easily visualize and comprehend data. Plotly provides a variety of plot types such as line charts, scatter plots, histograms, cox plots, etc.

Read more about seaborn @ Python Plotly Tutorial

### Visualizing Violin Plots using Seaborn Library

The first code shown below shows how to create a horizontal violin plot in Seaborn. We’re making use of the tips dataset from the seaborn library. We send the ‘total bill’ column to the `sns.violinplot()` method and utilize the palette to make it green.

```import seaborn as sns
sns.set_theme(style="whitegrid")
T  = sns.load_dataset("tips")
Ax = sns.violinplot(x=T["total_bill"], palette="Greens")
```

The second code demonstrates how two variables can be combined to create a vertical violin plot. We pass the data for the visualization and the color palette as “cool warm.”

```Ax = sns.violinplot(x="day", y="total_bill", data=T, palette="coolwarm")
```

### Visualizing Violin Plots using Matplotlib Library

To plot Violin plots using matplotlib, we will be creating a normal distribution and passing the data to `plt.violinplot` function along with some other properties of the violin plot.

```import matplotlib.pyplot as plt
import numpy as np

np.random.seed(10)
D1 = np.random.normal(100, 10, 200)
D2 = np.random.normal(80, 30, 200)
data_to_plot = [D1, D2,]
fig = plt.figure()
plt.violinplot(data_to_plot,showmeans=True, showextrema=True, showmedians=True)
plt.show()
```

### Visualizing Violin Plots using Plotly Library

The violin plot is plotted using the tips dataset which is demonstrated in the code mentioned below:

```import plotly.express as px

df = px.data.tips()
fig = px.violin(df, y="total_bill")
fig.show()
```

## Conclusion

Congratulations! You just learned how to plot Violin plots in the Python programming language. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

Thank you for taking your time out! Hope you learned something new!! 😄