How to add vertical grid lines in a grouped boxplot in Seaborn?
Last Updated :
04 Oct, 2024
Grouped boxplots allows to observe how distributions vary across different categories or subgroups. One common feature that often enhances visualizations, especially in complex plots, is adding vertical grid lines. Grid lines make it easier to follow data points across the x-axis, improving readability and aiding in comparison. In this article, we will focus on adding vertical grid lines to a grouped boxplot using Seaborn.
Why Add Vertical Grid Lines in Grouped Boxplots?
A grouped boxplot compares the distributions of multiple subgroups within a dataset, allowing for better understanding of categorical relationships. This visualization technique is particularly helpful when analyzing data split by more than one categorical variable, such as gender and education level, or year and product type.
- Vertical grid lines in a grouped boxplot improve readability by making it easier for the viewer to trace values along the x-axis.
- In some cases, grouped boxplots can be visually overwhelming due to multiple categories and subcategories.
- The grid lines provide a guide for comparing groups side by side and enhance the precision with which a viewer can interpret the relationships between different groups.
Creating a Grouped Boxplot in Seaborn
To create a grouped boxplot, we can use Seaborn’s boxplot() function and the hue parameter to split the data by subgroups. Here’s how to create a basic grouped boxplot with our sample dataset:
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Creating a larger sample dataset
data = pd.DataFrame({
'Department': ['Sales', 'Sales', 'Sales', 'HR', 'HR', 'HR', 'IT', 'IT', 'IT', 'Marketing', 'Marketing', 'Marketing'] * 2,
'Position': ['Junior', 'Mid', 'Senior', 'Junior', 'Mid', 'Senior', 'Junior', 'Mid', 'Senior', 'Junior', 'Mid', 'Senior'] * 2,
'Salary': [42000, 52000, 62000, 48000, 54000, 67000, 45000, 55000, 68000, 43000, 51000, 63000,
44000, 53000, 60000, 47000, 56000, 70000, 46000, 57000, 69000, 42000, 54000, 65000]
})
# Grouped Boxplot
sns.set(style="whitegrid")
sns.boxplot(x='Department', y='Salary', hue='Position', data=data)
plt.title('Salary Distribution by Department and Position')
plt.xlabel('Department')
plt.ylabel('Salary ($)')
plt.show()
Output:
vertical grid lines in a grouped boxplotIn this plot, the hue parameter allows us to group the data by 'Position' (Junior, Mid, Senior), with different colors representing each subgroup. This grouped boxplot allows us to compare salaries across departments and positions.
Customizing the Appearance of the Boxplot
Seaborn provides a variety of customization options that allow you to change the appearance of your plot. For example, you can change the color palette, adjust the aspect ratio, or modify the axis labels to suit your needs:
Python
# Customized Boxplot
sns.set(style="whitegrid")
sns.boxplot(x='Department', y='Salary', hue='Position', data=data, palette="Set3")
plt.title('Salary Distribution Across Departments and Positions')
plt.xlabel('Department')
plt.ylabel('Salary ($)')
plt.show()
Output:
vertical grid lines in a grouped boxplotThe palette parameter allows us to set a custom color scheme, while the title, xlabel, and ylabel functions modify the plot’s title and axis labels.
Adding Vertical Grid Lines in a Grouped Boxplot
Adding vertical grid lines to your grouped boxplot can greatly improve its readability. Fortunately, this is easily achievable using Matplotlib's axvline method or by enabling grid lines through plt.grid().
To add vertical grid lines, use the following approach:
Python
# Adding Vertical Grid Lines
sns.set(style="whitegrid")
ax = sns.boxplot(x='Department', y='Salary', hue='Position', data=data, palette="Set2")
plt.grid(True, axis='x') # Enable vertical grid lines
plt.title('Salary Distribution with Vertical Grid Lines')
plt.xlabel('Department')
plt.ylabel('Salary ($)')
plt.show()
Output:
vertical grid lines in a grouped boxplot Here, we’ve used plt.grid(True, axis='x') to enable vertical grid lines. You can control the visibility of the grid on specific axes by setting axis='x' for vertical grid lines or axis='y' for horizontal grid lines.
We can further customize the appearance of the vertical grid lines, Matplotlib allows you to modify the grid’s line style, color, and thickness:
Python
# Customizing Vertical Grid Lines
sns.set(style="whitegrid")
ax = sns.boxplot(x='Department', y='Salary', hue='Position', data=data, palette="Set1")
plt.grid(True, axis='x', linestyle='--', linewidth=0.7, color='gray')
plt.title('Customized Vertical Grid Lines in Grouped Boxplot')
plt.xlabel('Department')
plt.ylabel('Salary ($)')
plt.show()
Output:
vertical grid lines in a grouped boxplotIn this example, we’ve changed the linestyle to '--' (dashed), reduced the linewidth, and set the grid line color to gray.
Practical Example: Grouped Boxplot with Vertical Grid Lines
Let’s create a real-world example using a larger dataset. We’ll use the famous Titanic dataset to visualize the distribution of passenger fares across different classes and gender. Adding vertical grid lines will help in comparing fares between groups.
Python
# Load Titanic dataset
titanic = sns.load_dataset('titanic')
# Grouped Boxplot with Vertical Grid Lines
sns.set(style="whitegrid")
ax = sns.boxplot(x='class', y='fare', hue='sex', data=titanic, palette="Set3")
plt.grid(True, axis='x', linestyle='--', linewidth=0.7, color='gray')
plt.title('Fare Distribution by Class and Gender (Titanic)')
plt.xlabel('Class')
plt.ylabel('Fare ($)')
plt.show()
Output:
vertical grid lines in a grouped boxplotThis grouped boxplot shows the distribution of fares by class (First, Second, Third) and gender (Male, Female). The vertical grid lines make it easier to follow and compare fare distributions across the different classes.
Conclusion
In this article, we explored the importance of adding vertical grid lines to grouped boxplots in Seaborn and demonstrated how to implement them to improve the readability and clarity of complex plots. Grouped boxplots are a powerful tool for visualizing how distributions vary across different categories, and the addition of vertical grid lines can make these comparisons much easier to interpret.
Similar Reads
Adding Grid lines to a Catplot in Seaborn
In Seaborn, catplot is a function mainly for visualizing categorical data. It combines a categorical plot with a FacetGrid, allowing us to explore relationships between variables across different categories. While catplot provides informative visualizations by default, adding grid lines can further
4 min read
Add Vertical and Horizontal Lines to ggplot2 Plot in R
In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language. Adding Vertical Line To R Plot using geom_vline() For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position. Syntax: geom_vline(xint
4 min read
How To Make Grouped Boxplot with Seaborn Catplot?
Prerequisite: seaborn A grouped boxplot is a boxplot where categories are organized in groups and subgroups. Whenever we want to visualize data in the group and subgroup format the Seaborn Catplot() plays a major role. The following example visualizes the distribution of 7 groups (called A to G) and
2 min read
How to Add Outline or Edge Color to Histogram in Seaborn?
Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas.Se
3 min read
How to Add Vertical Lines to a Distribution Plot
Vertical lines in distribution plots help emphasize specific values or thresholds within the data distribution, aiding in visualizing critical points or comparisons. In this article, we will explore three different/approaches to add vertical lines to a distribution plot in Python. Understanding Dist
3 min read
How to Make Grouped Boxplots with ggplot2 in R?
In this article, we will discuss how to make a grouped boxplot in the R Programming Language using the ggplot2 package. Boxplot helps us to visualize the distribution of quantitative data comparing different continuous or categorical variables. Boxplots consist of a five-number summary which helps i
3 min read
How to Annotate Bars in Grouped Barplot in Python?
A Barplot is a graph that represents the relationship between a categoric and a numeric feature. Many rectangular bars correspond to each category of the categoric feature and the size of these bars represents the corresponding value. Using grouped bar plots, Â we can study the relationship between m
3 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R
In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R  Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To
3 min read
How to Make Grouped Boxplot with Jittered Data Points in ggplot2 in R
In this article, we will see how to make use of ggplot2 package in R Programming Language to plot grouped boxplots with jittered data points. Grouped Boxplots help us visualize two or more features/variables in a single plot using the grouping variable in ggplot2. The jittered points are data points
3 min read
How To Add Regression Line Per Group with Seaborn in Python?
In this article, we will learn how to add a regression line per group with Seaborn in Python. Seaborn has multiple functions to form scatter plots between two quantitative variables. For example, we can use lmplot() function to make the required plot. What is Regression Line? A regression line is ju
1 min read