Open In App

How to add vertical grid lines in a grouped boxplot in Seaborn?

Last Updated : 04 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

grouped
vertical grid lines in a grouped boxplot

In 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:

bosplot
vertical grid lines in a grouped boxplot

The 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:

bosplot
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:

boxplot
vertical grid lines in a grouped boxplot

In 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:

boxplot
vertical grid lines in a grouped boxplot

This 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.


Next Article

Similar Reads