Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Horizontal Boxplots with Points using Seaborn in Python
Boxplots are one of the most popular tools for data visualization, mainly created using Seaborn, which provides a simple and powerful way to create both horizontal and vertical boxplots and other types of visualizations.
In this article, we will focus on how to create a horizontal boxplot with points using Seaborn in Python.
What is a Boxplot?
A boxplot is a graphical representation of a dataset that shows the distribution of data using quartiles, median, and outliers. The box in the middle represents the interquartile range (IQR), with whiskers extending to the minimum and maximum values within a certain distance from the median. Outliers are displayed as individual points outside the whiskers.
Creating Horizontal Boxplots with Points
We'll use the "tips" dataset from Seaborn, which contains information about restaurant bills and the day of the week ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load the tips dataset
tips = sns.load_dataset("tips")
print(tips.head())
total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4
Basic Horizontal Boxplot with Points
To create a horizontal boxplot with individual data points, we combine sns.boxplot() and sns.swarmplot() ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
tips = sns.load_dataset("tips")
# Create horizontal boxplot
plt.figure(figsize=(10, 6))
sns.boxplot(x="total_bill", y="day", data=tips, orient="h")
sns.swarmplot(x="total_bill", y="day", data=tips, color="black", size=3, orient="h")
# Add labels and title
plt.title("Total Bill by Day")
plt.xlabel("Total Bill ($)")
plt.ylabel("Day of Week")
plt.show()
Customized Horizontal Boxplot
We can enhance the plot by customizing colors, styles, and parameters ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
tips = sns.load_dataset("tips")
# Set style and color palette
sns.set_style("whitegrid")
sns.set_palette("husl")
# Create customized horizontal boxplot
plt.figure(figsize=(10, 6))
sns.boxplot(x="total_bill", y="day", data=tips, whis=[0, 100], width=0.6,
fliersize=5, orient="h")
sns.swarmplot(x="total_bill", y="day", data=tips, color="black", size=4, orient="h")
# Customize appearance
plt.title("Total Bill Distribution by Day", fontsize=14, fontweight="bold")
plt.xlabel("Total Bill ($)", fontsize=12)
plt.ylabel("Day of Week", fontsize=12)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Function | Description |
|---|---|---|
orient="h" |
Both functions | Creates horizontal orientation |
whis=[0, 100] |
boxplot() | Shows full data range |
width |
boxplot() | Controls box width |
size |
swarmplot() | Controls point size |
Alternative Approach Using stripplot()
Instead of swarmplot(), you can use stripplot() for a different point distribution ?
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
tips = sns.load_dataset("tips")
# Create horizontal boxplot with stripplot
plt.figure(figsize=(10, 6))
sns.boxplot(x="total_bill", y="day", data=tips, orient="h")
sns.stripplot(x="total_bill", y="day", data=tips, color="red", size=3,
orient="h", jitter=True)
plt.title("Total Bill by Day (with stripplot)")
plt.xlabel("Total Bill ($)")
plt.ylabel("Day of Week")
plt.tight_layout()
plt.show()
Conclusion
Horizontal boxplots with points using Seaborn effectively visualize data distributions across categories. Combine boxplot() with swarmplot() or stripplot() to show both summary statistics and individual data points for comprehensive data exploration.
