Open In App

How to Add Vertical Lines to a Distribution Plot

Last Updated : 19 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Vertical lines in distribution plots help emphasize specific values or thresholds within the data distribution, aiding in visualizing critical points or comparisons. In this article, we will explore three different/approaches to add vertical lines to a distribution plot in Python.

Understanding Distribution Plots and Vertical Lines

Distribution plots, such as kernel density estimates (KDE) or histograms, are graphical representations of the probability distribution of a dataset. They help visualize how data points are distributed across different values.

Vertical lines are often added to distribution plots to highlight specific points or values of interest. These lines can be used to mark thresholds, mean values, or any other relevant points on the distribution.

Example:

In a KDE plot created using Seaborn's kdeplot, vertical lines can be added using Matplotlib's plt.axvline function. This function takes parameters such as the x-coordinate of the line (x), the color (color), and the linestyle (linestyle). By specifying these parameters, you can add vertical lines to a distribution plot to emphasize certain aspects of the data distribution.

Techniques to Add Vertical Lines

Below are the possible approaches/methods to add vertical lines to a distribution plot.

Method 1: Using Seaborn's distplot with matplotlib.pyplot.axvline

In this approach, we are using Seaborn's distplot to create a distribution plot of random data. Then, we add vertical lines at specified points using Matplotlib's plt.axvline method.

Example:

Python
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Generate random data
np.random.seed(0)
data = np.random.normal(loc=0, scale=1, size=1000)

# Create the distribution plot
sns.distplot(data)

# Add vertical lines
plt.axvline(x=-1, color='red', linestyle='--')
plt.axvline(x=1, color='blue', linestyle='--')

plt.show()

Output:

EX1

Method 2: Using Seaborn's displot with plt.axvline and the data parameter

In this approach, we create a DataFrame with sample data and use Seaborn's displot to plot a kernel density estimate (KDE) of the data. Vertical lines are added at specific values using Matplotlib's plt.axvline method, with the data parameter specifying the column from the DataFrame.

Example:

Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Create a DataFrame with different data
data_df = pd.DataFrame({'values': [2, 3, 4, 5, 6, 7, 8, 9, 10]})

# Create the distribution plot using Seaborn's displot
sns.displot(data=data_df, x='values', kind='kde')

# Add vertical lines
plt.axvline(x=5, color='green', linestyle='--')
plt.axvline(x=7, color='purple', linestyle='--')

plt.show()

Output:

EX2

Method 3: Using Seaborn's kdeplot with plt.axvline

In this approach, we use Seaborn's kdeplot to plot a KDE of a sample dataset ('total_bill' from Seaborn's 'tips' dataset). Vertical lines are added at chosen points using Matplotlib's plt.axvline method to highlight specific values on the distribution plot.

Example:

Python
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

# Create the distribution plot using Seaborn's kdeplot
sns.kdeplot(data=tips['total_bill'], shade=True)

# Add vertical lines
plt.axvline(x=10, color='orange', linestyle='--')
plt.axvline(x=20, color='pink', linestyle='--')

plt.show()

Output:

EX3

Conclusion

In conclusion, adding vertical lines to distribution plots is a powerful technique for highlighting key data points or comparisons. Through the explored methods using Seaborn and Matplotlib, users can effectively annotate and highlight critical aspects of the data distribution, enhancing the interpretability and insightfulness of their visualizations.


Next Article

Similar Reads