Byteli

Data Visualization: `hue` in Seaborn

Photo by Robert Katzki on Unsplash

The hue parameter in Seaborn allows for the seamless integration of categorical variables, introducing a spectrum of colors that not only enhances the aesthetic appeal of your plots but also provides a powerful tool for conveying intricate relationships within your data. In this blog, we will embark on a journey to explore the nuances of using ‘hue’ in Seaborn, unlocking its potential to reveal insights, distinguish patterns, and elevate the clarity of your visual narratives.

📊 Usage

Firstly, We will load tips dataset from the online repository in Seaborn. This dataset contains the following data:

One waiter recorded information about each tip he received over a period of a few months working in one restaurant

1import seaborn as sns
2import matplotlib.pyplot as plt
3
4# Load sample data
5data = sns.load_dataset("tips")

Like always, We will inspect our data then:

 1data.info()
 2
 3"""
 4<class 'pandas.core.frame.DataFrame'>
 5RangeIndex: 244 entries, 0 to 243
 6Data columns (total 7 columns):
 7 #   Column      Non-Null Count  Dtype
 8-- -----     ------------- -----
 9 0   total_bill  244 non-null    float64
10 1   tip         244 non-null    float64
11 2   sex         244 non-null    category
12 3   smoker      244 non-null    category
13 4   day         244 non-null    category
14 5   time        244 non-null    category
15 6   size        244 non-null    int64
16dtypes: category(4), float64(2), int64(1)
17memory usage: 7.4 KB
18"""

According to the dataset description, each variable has the following meaning:

total_bill: bill in dollars, tip: tip in dollars, sex: sex of the bill payer, smoker: whether there were smokers in the party, day: day of the week, time: time of day, size: size of the party.

Scatter Plots

In this example, we’ll compare a simple scatter plot with and without hue to showcase how it can enhance our understanding of the data.

Without hue

1sns.scatterplot(x="total_bill", y="tip", data=data)

Scatter Plot Without Hue

With hue

1sns.scatterplot(x="total_bill", y="tip", hue="time", data=data)

Scatter Plot With Hue

In this example, we compare two scatter plots side by side. The left plot (without hue) shows a basic scatter plot of total bill amount vs. tip without distinguishing different days. The right plot (with hue) introduces the ‘day’ column as the hue parameter, coloring the points based on the days of the week.

By using hue, we can observe how the relationship between total bill and tip varies across different time. This additional categorical information enhances the visualization, making it easier to differentiate patterns and trends within the data.

Bar Plots

1sns.barplot(x="day", y="total_bill", hue="sex", data=data)

Bar Plot With Hue

Box Plots

1sns.boxplot(x="day", y="total_bill", hue="sex", data=data)

Box Plot With Hue

Violin Plots

1sns.violinplot(x="day", y="total_bill", hue="sex", data=data)

Violin Plot With Hue

Count Plots

1sns.countplot(x="day", hue="sex", data=data)

Count Plot With Hue

🤔 When to Use and When Not to Use hue?

The use of hue is particularly beneficial in the following cases:

While the hue parameter in Seaborn can be a powerful tool for enhancing visualizations by incorporating an additional categorical variable, there are situations where it might not be necessary or could potentially lead to confusion:

Conclusion

In conclusion, Seaborn’s hue parameter is a dynamic tool that breathes vitality into visualizations, providing nuanced insights across plot types, from scatter plots to count plots. By introducing an additional layer of categorical information, hue unveils hidden patterns and relationships. However, its use should be judicious, especially when simplicity is crucial.

#data visualization #seaborn #python #data science


Reply to this post by email ↪