• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Add Mean/Median Line to a Seaborn Displot

datavizpyr · August 23, 2021 ·

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

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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version