Introduction
In this article, we will go through the tutorial for the Seaborn Violin plot for visualizing data for your data science and machine learning projects. We will under the syntax of sns.violinplot() function of Seaborn library and then see various examples of creating violin plot using the same.
Seaborn Violin Plot Tutorial
A violin plot is a visualization that shows the spread of quantitative data with different categorical variables. Violin plot uses kernel density estimation for displaying underlying distribution. Violin plot is generally used in cases where multiple distributions of data are to be visualized.
In the Seaborn library, there is a function sns.violinplot() that can be used to create violin plots. Let us see the syntax.
Syntax of Seaborn violinplot()
seaborn.violinplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw=’scott’, cut=2, scale=’area’, scale_hue=True, gridsize=100, width=0.8, inner=’box’, split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, kwargs)**
- x, y, hue : names of variables in data or vector data, optional
Here the input variables that will be used in data for the purpose of plotting is provided.
- data : DataFrame, array, or list of arrays, optional
This parameter takes the data based on which the visualization will be plotted.
- order, hue_order : lists of strings, optional
This is the order for categorical variables used in the plot.
- scale : {“area”, “count”, “width”}, optional
The method used to scale the plot. It has multiple values for usage.
- scale_hue : bool, optional
When nesting violins using a hue variable, this parameter determines whether the scaling is computed within each level of the major grouping variable (scale_hue=True) or across all the violins on the plot (scale_hue=False).
- gridsize : int, optional
These are the points used to calculate the kernel density estimate for the plot.
- inner : {“box”, “quartile”, “point”, “stick”, None}, optional
This parameter helps in defining the inner points of a violin plot.
- orient : “v” | “h”, optional
This parameter can help in deciding the orientation of the plot. It can be either horizontal or vertical.
- linewidth : float, optional
This is the width of gray lines used inside the plot.
- color : matplotlib color, optional
This will help in specifying the color for all the elements of the plot.
- palette : palette name, list, or dict
The palette defines the colors used for different levels of the plot with a variety of hues.
- ax : matplotlib Axes, optional
These are the axes on which we build the plot.
1st Example – Single Horizontal Violin Plot in Seaborn
The first example shows how we can build a horizontal violin plot in Seaborn. We are using the tips dataset provided by seaborn library.
We pass the ‘total_bill’ column to the sns.violinplot() function and along with this, we use the palette parameter for coloring it green.
import seaborn as sns
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x=tips["total_bill"], palette="Greens")
2nd Example – Vertical Violin Plot Grouped by Categorical Variable
The second example shows how two variables can be used together for building a vertical violin plot. We pass the data for plotting the visualization along with the color palette as “coolwarm”.
ax = sns.violinplot(x="day", y="total_bill", data=tips, palette="coolwarm")
3rd Example – Nested Violin Plot with Categorical Variables
This example will be showing a nested violin plot with the help of hue parameter.
We pass the “smoker” field of the dataset to the hue parameter of sns.violinplot() function.
ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
data=tips, palette="coolwarm_r")
4th Example – Comparison using Split Violins
The 4th example of violin plot shows the spread of data that helps in comparison by splitting the violin plot. For splitting, we use the split parameter and it’s passed as True.
ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
data=tips, palette="Blues", split=True)
- Also Read – Matplotlib Violin Plot – Tutorial for Beginners
Conclusion
This is the end of this seaborn tutorial. In this, we learned about violin plot, its syntax, and the types of violin plot that can be built using the seaborn library. The tutorial also covered various examples to show the usage of various parameters for the violin plot.
Reference: https://seaborn.pydata.org/
-
I am Palash Sharma, an undergraduate student who loves to explore and garner in-depth knowledge in the fields like Artificial Intelligence and Machine Learning. I am captivated by the wonders these fields have produced with their novel implementations. With this, I have a desire to share my knowledge with others in all my capacity.
View all posts