• 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 Vertical/Horizontal Lines to Subplots with Seaborn

datavizpyr · December 29, 2021 ·

Last updated on January 20, 2022

In this tutorial, we will learn how to add vertical or horizontal lines to “small multiples” i.e. multiple subplots of similar kind using Seaborn’s refline() function (h/t to Chris Moffitt of @pbpython). Vertical/Horizontal lines are often useful to show where the mean or median values of the variables of interest and in a simple plot we can use axvline/axhline to add the lines.

Let us get started with loading the packages needed.

import seaborn as sns
import matplotlib.pyplot as plt

We will use Palmer penguin dataset available from Seaborn’s built-in datasets.

penguins = sns.load_dataset("penguins")

Small multiples with Seaborn’s displot()

We can make a small multiple or facetted plot, in this example multiple histograms, using Seaborn’s displot() function with “col” argument. In this example, we are plotting the distributions of flipper lengths of different penguin species.

plt.figure(figsize=(12,6))
g = sns.displot(data = penguins,
               x = "flipper_length_mm",
               col = "species")
plt.savefig("Seaborn_small_multiples_With_displot.png",
                    format='png',dpi=150)
Seaborn small multiples with displot()
Seaborn small multiples with displot()

Seaborn’s refline() function to add horizontal/vertical lines in subplots

To add a horizontal and vertical line we can use Seaborn’s refline() function with x and y y co-ordinates for the locations of the horizontal and vertical lines.

plt.figure(figsize=(12,6))
g.refline(x = penguins.flipper_length_mm.mean(),
          y = 20,
          color = "red",
         lw = 3)
plt.savefig("Seaborn_small_multiples_refline_example.png",
                    format='png',dpi=150)

Here we have the small multiples with horizontal and vertical lines at the specified locations in “red” color”.

Seaborn refline: add vertical and horizontal lines to subplots
Seaborn refline: add vertical and horizontal lines to subplots

To add horizintal/vertical lines in different colors or different shapes, we can use two reflline() statements, one for horizontal and one for vertical lines as shown below.

g.refline(x=penguins.flipper_length_mm.mean(),
          color = "red",
          lw = 3)
g.refline(y = 20,
          color = "blue",
          lw = 3)
plt.savefig("Seaborn_small_multiples_refline_add_vertical_horizontal_lines.png",
                    format='png',dpi=150)
Add vertical and Horizontal Lines with Seaborn refline
Add vertical and Horizontal Lines to “Small multiples” with Seaborn refline

An additional plot of the similar flavor is to add the group specific horizontal or vertical lines instead of common ones. I may be wrong about it, but it does not look like Seaborn’s refline() can do that readily yet.

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 Vertical/Horizontal 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