Visualize Distributions Using Seaborn
Seaborn is a powerful Python visualization library built on top of Matplotlib.
It simplifies the process of creating attractive and informative statistical
graphics, including plots for visualizing data distributions.
Installing Seaborn
If you already have Python and pip installed, you can install Seaborn by
running:
pip install seaborn
If you are working in Jupyter Notebook, use:
!pip install seaborn
Creating Distribution Plots (Displots)
A displot (short for distribution plot) is used to visualize the
distribution of a dataset. It can display histograms, kernel density
estimates (KDE), or both. Import Required Libraries
Importing Matplotlib
To use Matplotlib for plotting, import the pyplot module with the following
command:
import matplotlib.pyplot as plt
Import Seaborn
To import the Seaborn library into your code, use the following command:
Basic Displot Example (Histogram)
import matplotlib.pyplot as plt
import seaborn as sns
sns.displot([0, 1, 2, 3, 4, 5])
plt.show()
Output:
The output will be a histogram plot:
The histogram will have bins representing the counts of values in the list
[0, 1, 2, 3, 4, 5].
Since all numbers appear once, each bar will have a height of 1 for each
number (each value appears only once).
The x-axis will range from around 0 to 5.
The y-axis will range from 0 to 1 or slightly higher depending on binning
behavior.
Displot with KDE Curve (Without Histogram)
To plot only the KDE (Kernel Density Estimate) curve without the histogram,
use the kind="kde" argument:
import matplotlib.pyplot as plt
import seaborn as sns
sns.displot([0, 1, 2, 3, 4, 5], kind="kde")
plt.show()
Output:
The output will be a smooth KDE curve (similar to a smoothed histogram)
representing the distribution of the given numbers [0, 1, 2, 3, 4, 5].
Since these numbers are evenly spaced, the KDE plot will look like a smooth
bump rising from near 0 and peaking around the middle of the range, then
decreasing.
There is no text output, just a graphical plot.
Note:
In this tutorial, we’ll use:
sns.displot(arr, kind="kde")
to visualize random distributions.
More topic in Numpy