What are distplots in Seaborn?
Overview
We use a displot (also known as a distribution plot) to represent data in histogram form. It is a univariant set of collected data, which means the data distribution of one variable will be shown against another variable.
In Python, we use the Seaborn library with Matplotlib for data visualization. It provides an interface that allows users to present data in various descriptive and eye-catching graphics in statistical form. Seaborn will enable us to deliver data using the types below:
- displot
- jointplot
- pairplot
- rug plot
Now let’s dive deeper.
In Seaborn, we use the seaborn.displot() function to display one data variable's distribution against the other variable as density distribution.
Example
The function seaborn.displot() takes data variable and color as arguments and returns data in distribution form.
# import librariesimport numpy as npimport seaborn as snsimport matplotlib.pyplot as plt# code to generate random numbersdata = np.random.randn(500)# plot distplotsns.displot(data, color="red")plt.title("Data Distribution")plt.savefig('output/graph.png')
Explanation
- Lines 2–4: We'll import the
numpy,seaborn, andmatplotliblibraries in the program. - Line 6: We'll use the
np.random.randn(500)to generate an array of random numbers. In this line, it will create 500 random values. - Line 8: We'll use the
seab.displot(data)function to generate a distribution plot. We use theseab.displot(data)function to plot a distribution plot of data in the form of a histogram. - Line 9: We'll use the
plt.title()function to set the graph title to"Data Distribution". - Line 10: We'll use the
plt.savefig()function to save the graph generated above asgraph.pngin the output directory.