Skip to main content
Practice

Grouping and Hue for Comparisons in Seaborn

One of Seaborn’s most powerful features is the ability to compare subgroups within a dataset using the hue parameter.

Adding hue lets you separate data into categories and automatically color them — making comparisons clear and visually engaging.


Why Hue is Useful

  • Adds a new layer of information without needing multiple plots.
  • Highlights category-level patterns and differences.
  • Works seamlessly across functions like barplot, scatterplot, and lineplot.

Comparing Categories in a Scatterplot

You can apply hue in scatterplots to visually compare categories within your data.

Scatterplot with Hue
import seaborn as sns
import matplotlib.pyplot as plt

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

# Scatterplot with hue for gender
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex")

plt.title("Total Bill vs Tip by Gender")
plt.show()
  • hue="sex" automatically assigns distinct colors for male and female groups.
  • A legend is added by default to show which color represents each group.

Pro Tip

You can also customize the palette to control your color scheme when using hue:

Custom Color Palette
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", palette="Set2")

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.