How to Add Vertical Lines to a Distribution Plot
Last Updated :
19 Jun, 2024
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:

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:

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:

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.
Similar Reads
How to Plot a Weibull Distribution in R
In this article, we will discuss what is Weibull Distribution and what are the Properties of Weibull Distribution and how we implement the Weibull Distribution in R Programming Language.Introduction to Weibull DistributionThe Weibull Distribution is a continuous probability distribution commonly use
4 min read
How to Add a Diagonal Line to a Plot Using R
Creating plots is a fundamental aspect of data visualization in R Programing Language. Sometimes, it's useful to add reference lines, such as diagonal lines, to the plots for better interpretation of the data. Here, we will explore how to add a diagonal line to a plot using R. Importance of Diagonal
3 min read
Add Vertical and Horizontal Lines to ggplot2 Plot in R
In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language. Adding Vertical Line To R Plot using geom_vline() For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position. Syntax: geom_vline(xint
4 min read
How to plot a normal distribution with Matplotlib in Python?
Normal distribution, also known as the Gaussian distribution, is a fundamental concept in probability theory and statistics. It is a symmetric, bell-shaped curve that describes how data values are distributed around the mean. The probability density function (PDF) of a normal distribution is given b
4 min read
How to Add Text Labels to a Histogram in Plotly
Plotly is a powerful and versatile library for creating interactive visualizations in Python. Among its many features, Plotly allows users to create histograms, which are essential for visualizing the distribution of numerical data. Adding text labels to histograms can enhance the interpretability o
3 min read
Plot t Distribution in R
The t-distribution, also known as the Student's t-distribution is a type of probability distribution that is used to perform sampling of a normally distributed distribution where the sample size is small and the standard deviation of the input distribution is unknown. The distribution normally forms
4 min read
Add Horizontal or Vertical Line in Plotly Using R
Plotly is a powerful and versatile plotting library in R that enables the creation of interactive and publication-quality visualizations. One common requirement in data visualization is to add reference lines, such as horizontal or vertical lines, to a plot to highlight specific values or thresholds
4 min read
How to add vertical grid lines in a grouped boxplot in Seaborn?
Grouped boxplots allows to observe how distributions vary across different categories or subgroups. One common feature that often enhances visualizations, especially in complex plots, is adding vertical grid lines. Grid lines make it easier to follow data points across the x-axis, improving readabil
5 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R
In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R  Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To
3 min read
How to Plot a Vertical Line on a Time Series Plot in Pandas
When working with time series data in Pandas, it is often necessary to highlight specific points or events in the data. One effective way to do this is by plotting vertical lines on the time series plot. In this article, we will explore how to plot a vertical line on a time series plot in Pandas, co
3 min read