Plotting Violin Plots in Python using the Seaborn Library
This tutorial will show you how to use violin plots in Python. To do this, we use the Seaborn Library.
Violin plots are a way of plotting numeric data. They can be considered as a combination of a box plot and kernel density (KDE) plot.
For the sake of this example, we will be using the very popular, ‘iris
‘ dataset. The iris dataset downloads directly during the execution of the program. So, a good internet connection is needed.
The iris dataset comprises some information about flowers.
Code and its Explanation
# Importing the required libraries import seaborn as sns import matplotlib.pyplot as plt # Loading the iris dataset and storing it in the variable, 'dataset' dataset = sns.load_dataset("iris") # We will only consider the top 20 values of the dataset for this example dataset.head(20) #Creating the violin plot sns.violinplot(x="sepal_length",data=dataset) #Showing the plot plt.show()
First, we import the required libraries, i.e., seaborn
and matplotlib
.
Next, we download the iris dataset and store it in the variable, ‘dataset
‘. During this step, the program requires an active internet connection.
Since this is only an example, we will use only the top 20 entries of the dataset. To do this, we use the head()
function.
Next, we create a violin-plot of the ‘sepal_length
‘ data from the iris dataset.
Finally, we show the plot using the ‘show()
‘ function.
OUTPUT
Here you can see the output we got from the above program.
Leave a Reply