Home » How to Plot a Distribution in Seaborn (With Examples)

How to Plot a Distribution in Seaborn (With Examples)

by Tutor Aspire

You can use the following methods to plot a distribution of values in Python using the seaborn data visualization library:

Method 1: Plot Distribution Using Histogram

sns.displot(data)

Method 2: Plot Distribution Using Density Curve

sns.displot(data, kind='kde')

Method 3: Plot Distribution Using Histogram & Density Curve

sns.displot(data, kde=True)

The following examples show how to use each method in practice.

Example 1: Plot Distribution Using Histogram

The following code shows how to plot the distribution of values in a NumPy array using the displot() function in seaborn:

import seaborn as sns
import numpy as np

#make this example reproducible
np.random.seed(1)

#create array of 1000 values that follow a normal distribution with mean of 10
data = np.random.normal(size=1000, loc=10)

#create histogram to visualize distribution of values
sns.displot(data)

The x-axis displays the values in the distribution and the y-axis displays the count of each value.

To change the number of bins used in the histogram, you can specify a number using the bins argument:

import seaborn as sns
import numpy as np

#make this example reproducible
np.random.seed(1)

#create array of 1000 values that follow a normal distribution with mean of 10
data = np.random.normal(size=1000, loc=10)

#create histogram using 10 bins
sns.displot(data, bins=10)

Example 2: Plot Distribution Using Density Curve

The following code shows how to plot the distribution of values in a NumPy array using a density curve:

import seaborn as sns
import numpy as np

#make this example reproducible
np.random.seed(1)

#create array of 1000 values that follow a normal distribution with mean of 10
data = np.random.normal(size=1000, loc=10)

#create density curve to visualize distribution of values
sns.displot(data, kind='kde')

The x-axis displays the values in the distribution and the y-axis displays the relative frequency of each value.

Note that kind=’kde’ tells seaborn to use kernel density estimation, which produces a smooth curve that summarizes the distribution of values for a variable.

Example 3: Plot Distribution Using Histogram & Density Curve

The following code shows how to plot the distribution of values in a NumPy array using a histogram with a density curve overlaid:

import seaborn as sns
import numpy as np

#make this example reproducible
np.random.seed(1)

#create array of 1000 values that follow a normal distribution with mean of 10
data = np.random.normal(size=1000, loc=10)

#create histogram with density curve overlaid to visualize distribution of values
sns.displot(data, kde=True)

The result is a histogram with a density curve overlaid.

Note: You can find the complete documentation for the seaborn displot() function here.

Additional Resources

The following tutorials explain how to perform other common tasks using seaborn:

How to Add a Title to Seaborn Plots
How to Change Font Size in Seaborn Plots
How to Adjust Number of Ticks in Seaborn Plots

You may also like