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

datavizpyr · December 29, 2021 ·

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

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