| by Arround The Web | No comments

Seaborn Violin Plot

In Python, developers often need to plot the statistical graph for different data frames. For that corresponding purpose, Python provides several libraries, such as the “seaborn” library. The “seaborn” is a fantastic Python visualization library that gives default color palettes and styles to make the statistical graphs more attractive.

In this guide, we will talk about:

What is a Violin Plot?

The “violin” graph is used to display the distribution of quantitative data across different levels of one or multiple categorical variables.

Seaborn Violin Plot in Python

To draw a Seaborn violin plot in Python, first, import the required libraries, such as “seaborn” and “matplotlib.pyplot” respectively:

import seaborn
import matplotlib.pyplot as plt

 
Next, use the “set()” method that takes “style” parameters and the “whitegrid” as its value that is used to adjust the background pattern of the graph. After that, load the “tips” dataset which is the built-in data set of the seaborn library, and store it in the “dataframe” variable. Lastly, display the loaded dataset:

seaborn.set(style = 'whitegrid')
dataframe = seaborn.load_dataset("tips")
dataframe

 
As you can see, the specified dataset has been loading successfully:


Next, create a violin plot through the seaborn library. For that purpose, call the “violinplot()” method along with the required parameters:

seaborn.violinplot(x = "time", y = "tip", hue = "sex",
                data = dataframe, palette = "pastel", dodge = True)

 
Here:

    • x = “time”” indicates the x-axis label value.
    • y = “tip”” shows the y-axis label value.
    • hue = “sex”” parameter indicates which column decides the type of color.
    • data” parameter takes the input dataset as a value.
    • dataframe” is the variable that holds the specified dataset.
    • palette” argument indicates the color palette.
    • dodge” parameter with the “True” values is used to display every subgroup separately from others.

Lastly, call the “plt.show()” method for displaying the resultant graph:

plt.show()

 
Output


We have compiled the easiest way to draw the Seaborn violin plot in Python.

Conclusion

In Python, the “seaborn.violin()” method is utilized to display the quantitative data separately across different levels of one or several categorical variables. The “seaborn” library gives default color palettes and styles to make the statistical graphs more attractive. This guide illustrated the Seaborn violin graph in Python.

Share Button

Source: linuxhint.com

Leave a Reply