Hey, folks! In our Seaborn tutorial, we will be focusing on Seaborn Kdeplot.
Kdeplot
is a Kernel Distribution Estimation Plot which depicts the probability density function of the continuous or non-parametric data variables i.e. we can plot for the univariate or multiple variables altogether. Using the Python Seaborn module, we can build the Kdeplot with various functionality added to it.
In order to use the Seaborn module, we need to install and import the module using the below command:
pip install seaborn
import seaborn
The seaborn.kdeplot() function is used to plot the data against a single/univariate variable. It represents the probability distribution of the data values as the area under the plotted curve.
Syntax:
seaborn.kdeplot(data)
Example 1:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data)
plt.show()
In the above example, we have generated some random data values using the numpy.random.randn() function.
Output:
Example 2:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
data = np.random.randn(200)
res = sn.kdeplot(data,color='green',shade=True)
plt.show()
In the above example, we have highlighted the plot using the parameter - ‘shade
’ to highlight the area under the curve. Further, we can set different colors to the plot using the parameter - ‘color
’.
Output:
Seaborn Kdeplots can even be used to plot the data against multiple data variables or bivariate(2) variables to depict the probability distribution of one with respect to the other values.
Syntax:
seaborn.kdeplot(x,y)
Thus, the distribution is represented as a contour plot depicting the relationship of the distribution between the two data variables.
Example:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],color='blue',shade=True)
plt.show()
Output:
We can plot the Kdeplots along the y-axis using the below syntax:
Syntax:
seaborn.kdeplot(data,vertical=True)
Thus, by setting the ‘vertical
’ parameter to True, we can plot the distribution against the y-axis.
Example:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],vertical=True,color='blue',shade=True)
plt.show()
Output:
Different color palettes can be used along with the Seaborn plots to visualize the data in a better manner using the ‘cmap
’ parameter.
Different types of color palettes are available at Matplotlib Colormap.
Syntax:
seaborn.kdeplot(data,cmap)
Example:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
res = sn.kdeplot(data['mpg'],data['qsec'],shade=True,cmap="Purples_d")
plt.show()
Output:
The two shaded Bivariate Kdeplots help in understanding the variation of the data in terms of the probability distribution of the bivariate group of data variables.
Example:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d")
res = sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Blues")
plt.show()
Output:
A colorbar
maps the pictorial representation of values against the original data values and helps visualize the data in a better manner.
Syntax:
seaborn.kdeplot(data,cbar=True)
Example:
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
import pandas
data = pandas.read_csv("C:/mtcars.csv")
sn.set(style='dark',)
res=sn.kdeplot(data['hp'],data['cyl'],shade=True,cmap="Purples_d",cbar=True)
plt.show()
Output:
Seaborn module is purely built upon the Matplotlib module and the combination is extensively used to visualize the data in different forms.
I would strongly recommend the readers to go through Python Matplotlib Tutorial for a better understanding about the basics of data visualization.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.