• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How To Add Mean/Median Line to a Seaborn Displot

datavizpyr · August 23, 2021 ·

Last updated on August 22, 2025

In this tutorial, we will learn how to add mean or median vertical line to a plot made with Seaborn’s displot() function. Seaborn’s displot() offers capability to visualize the univariate or bivariate distribution of data. Here we will make a histogram with Seaborn’s displot() and then see how to add median line to the histogram,

Let us load the libraries needed.

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

👉 Want more? Explore the full Seaborn Tutorial Hub with 35+ examples, code recipes, and best practices.

Histogram with Seaborn’s displot()

We will using penguins data available from seaborn to make histogram with displot (not distplot) function.

penguins = sns.load_dataset("penguins")
sns.displot(penguins,
            x = "flipper_length_mm",
            bins=30,
            height=8)
plt.xlabel("Flipper Length", size=14)
plt.ylabel("Count", size=14)
plt.savefig("Seaborn_displot_histogram_Python.png")
Histogram with Seaborn displot()
Histogram with Seaborn displot()

Add Mean line to Histogram with axvline()

We will use Matplotlib’s axvline() function to add mean line to the histogram made with Seaborn’s displot(). We also specify color argument to make the mean line in red color.

sns.displot(penguins,
            x = "flipper_length_mm",
            bins=30,
           height=8)
plt.xlabel("Flipper Length", size=14)
plt.ylabel("Count", size=14)
plt.axvline(x=penguins.flipper_length_mm.mean(),
            color='red')
plt.savefig("Seaborn_displot_histogram_with_mean_line_Python.png")
Seaborn displot with median line
Seaborn displot with median line

Customizing Mean line to Histogram with axvline()

To further customize the mean/median line to the histogram we use line type with “ls” and line width using “lw” to make a thicker dotted median line.

sns.displot(penguins,
            x = "flipper_length_mm",
            bins=30,
           height=8)
plt.xlabel("Flipper Length", size=14)
plt.ylabel("Count", size=14)
plt.axvline(x=penguins.flipper_length_mm.median(),
            color='blue',
            ls='--', 
            lw=2.5)
plt.savefig("Seaborn_displot_histogram_with_median_line_Python.png")
Seaborn displot with median line
Seaborn displot with median line

Related posts:

Boxplot with Color Palette Set3 SeabornHow To Use Seaborn Color Palette to Color Boxplot Seaborn Scatterplot: Change edgecolor and line widthHow To Change Edge Color on Seaborn Scatter Plot? Combine Two plots into one in SeabornHow to Combine Two Seaborn plots with Shared y-axis Visualizing Missing Data with Seaborn DisplotVisualizing Missing Data with Seaborn Heatmap and Displot

Filed Under: Python, Seaborn Tagged With: Seaborn displot, Seaborn displot mean line, Seaborn Displot Median Line

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version