How to plot two violin plot series on the same graph using Seaborn?

To plot two violin plot series on the same graph using Seaborn, we can use the hue parameter in the violinplot() method. This allows us to create separate violin plots for different categories within the same visualization.

Basic Violin Plot with Two Series

The most straightforward approach is using the hue parameter to split data by a categorical variable ?

import seaborn as sns
import matplotlib.pyplot as plt

# Set the figure size
plt.figure(figsize=(10, 6))

# Load an example dataset
tips = sns.load_dataset("tips")

# Create a violin plot with two series using hue
sns.violinplot(x="day", y="total_bill", hue="time", data=tips)

# Add title and labels
plt.title("Total Bill Distribution by Day and Time")
plt.xlabel("Day of Week")
plt.ylabel("Total Bill ($)")

# Display the plot
plt.show()

Customizing the Violin Plot

You can customize colors, split the violins, and adjust the appearance ?

import seaborn as sns
import matplotlib.pyplot as plt

# Set the figure size
plt.figure(figsize=(12, 6))

# Load dataset
tips = sns.load_dataset("tips")

# Create violin plot with custom styling
sns.violinplot(x="day", y="total_bill", hue="time", data=tips,
               split=True, palette="Set2", inner="quart")

# Customize the plot
plt.title("Split Violin Plot: Total Bill by Day and Time", fontsize=16)
plt.xlabel("Day of Week", fontsize=12)
plt.ylabel("Total Bill ($)", fontsize=12)
plt.legend(title="Time", bbox_to_anchor=(1.05, 1), loc='upper left')

plt.tight_layout()
plt.show()

Multiple Violin Plots Side by Side

For comparing different variables, you can create subplots with separate violin plots ?

import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
tips = sns.load_dataset("tips")

# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(15, 6))

# First violin plot
sns.violinplot(x="day", y="total_bill", hue="time", data=tips, ax=axes[0])
axes[0].set_title("Total Bill Distribution")
axes[0].set_xlabel("Day")
axes[0].set_ylabel("Total Bill ($)")

# Second violin plot
sns.violinplot(x="day", y="tip", hue="time", data=tips, ax=axes[1])
axes[1].set_title("Tip Distribution")
axes[1].set_xlabel("Day")
axes[1].set_ylabel("Tip ($)")

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example Values
hue Groups data into separate violins "time", "sex", "smoker"
split Splits violins for comparison True/False
palette Color scheme for the violins "Set1", "viridis", "husl"
inner Interior representation "box", "quart", "point", "stick"

Conclusion

Use the hue parameter to create two violin plot series on the same graph. The split=True option provides better comparison between categories, while custom palettes and styling enhance visualization clarity.

Updated on: 2026-03-26T14:54:22+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements