Adding Vertical Lines with Seaborn’s axvline

Here’s a simple example of how to use axvline in Seaborn:

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("iris")

sns.histplot(data=data, x="sepal_width", kde=True)

plt.axvline(x=3.0, color='red', linestyle='--', label='Threshold')

plt.xlabel("Sepal Width")
plt.ylabel("Frequency")
plt.legend()

plt.show()

In this example, we load the Iris dataset, create a histogram of sepal widths, and then use plt.axvline to add a vertical line at a value of 3.0 on the x-axis. We customize the line’s appearance by specifying its color, linestyle, and label.

See also  How to Make a Countplot in Seaborn

The axvline function is particularly useful when you want to visually represent thresholds, critical values, or significant points on your plots. You can apply it to various types of Seaborn plots, including histograms, bar plots, and line plots.

Don’t forget to customize your vertical lines to match the context and aesthetics of your visualization. You can adjust colors, linestyles, and labels to make your plots more informative and visually appealing.

See also  How to Make a Kdeplot in Seaborn