• 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

Horizontal Boxplots with Points using Seaborn in Python

datavizpyr · December 31, 2019 ·

Horizontal boxplots are useful when you have lots of groups with longer names. If you make simple boxplots, the longer names would overlap on each other and make it impossible to read the group names.
Horizontal boxplots solve that problem easily.

In this post, we will see an example of making horizontal boxplots with data points overlayed on boxes.

Let us first load the libraries needed.

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

Generate data for making boxplots

We will generate data with three variables/groups for our example. Here we using Numpy’s random module to generate random numbers for each column.

 
np.random.seed(31)
# Generating Data
df = pd.DataFrame({
    'Africa': np.random.normal(40, 15, 100),
    'Asia': np.random.normal(60, 10,100),
    'Americas': np.random.normal(80, 5, 100)
})
print(df.head())

The data we generated is in wide form. We will use Pandas’ melt function to convert the dataframe in wide form to lonf form.

 
data_df = df.melt(var_name = 'continent',
                  value_name = 'lifeExp')
print(data_df.head())

Simple Horizontal boxplot in Python

With Seaborn, we can easily make horizontal boxplots by simply swapping x and y -variables.

 
# horizontal boxplot with Seaborn Python
sns.boxplot(y = "continent",
            x = "lifeExp",
           data = data_df)

Horizontal boxplot with jittered points in Python

To make horizontal boxplots with data points, we also need to sway x and y axis for the stripplot we make using Seaborn’s stripplot() function right after Seaborn’s boxplot().

 
plt.figure(figsize=(8.3,6))
# horizontal boxplot  Python
sns.boxplot(y = "continent",
            x = "lifeExp",
           data = data_df)
# horizontal stripplot Python
sns.stripplot(y = "continent",
              x = "lifeExp",
              color = 'black',
              alpha=0.3,
              data=data_df)
plt.ylabel("Continent", size=18)
plt.xlabel("LifeExp", size=18)

This is how the horizontal boxplots with points overlapped would like like. In this example, we have added transparency level to data points and also set the color of data points to be black inside stripplot() function.

Horizontal Boxplots with Points using Seaborn
Horizontal Boxplots with Points using Seaborn

Related posts:

Colored Boxplot with Bigger Points Using SeabornHow to Make Boxplots with Data Points using Seaborn in Python Horizontal Boxplots with SeabornHorizontal Boxplots with Seaborn in Python Boxplot with Catplot Seaborn PythonHow To Make Boxplots with Seaborn in Python? Grouped Boxplot with Jittered Data Points SeabornGrouped Boxplot with Jittered Points with Seaborn Python

Filed Under: Boxplot with points Seaborn, Python, Python Boxplot, Seaborn Tagged With: boxplot python, Horizontal boxplot Python

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