How to plot a jointplot with 'hue' parameter in Seaborn? (Matplotlib)

To create a jointplot with the hue parameter in Seaborn, we can use sns.jointplot() to visualize the relationship between two variables while coloring points by a third categorical variable.

Basic Jointplot with Hue

The hue parameter allows us to color-code data points based on different categories ?

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
x = np.linspace(0, 1, 5)

# Create dictionary with curve data
data_dict = {
    'y_sin': np.sin(x),
    'y_cos': np.cos(x),
    'y_linear': x,
}

# Create DataFrame
df = pd.DataFrame(data_dict)

# Create jointplot with hue parameter
jg = sns.jointplot(data=df, x="y_sin", y="y_cos", 
                   height=3.5, hue="y_linear")
plt.show()

Practical Example with Categorical Hue

A more realistic example using categorical data for the hue parameter ?

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

# Create sample dataset
np.random.seed(42)
n_samples = 100

# Generate data with categories
categories = ['Group A', 'Group B', 'Group C']
data = {
    'x': np.random.normal(0, 1, n_samples),
    'y': np.random.normal(0, 1, n_samples),
    'category': np.random.choice(categories, n_samples)
}

df = pd.DataFrame(data)

# Create jointplot with categorical hue
jg = sns.jointplot(data=df, x="x", y="y", hue="category", 
                   height=6, alpha=0.7)
plt.show()

Customizing the Jointplot

You can customize the jointplot appearance with additional parameters ?

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

# Create sample data
np.random.seed(123)
data = {
    'score1': np.random.normal(75, 15, 200),
    'score2': np.random.normal(80, 12, 200),
    'class': np.random.choice(['Math', 'Science', 'English'], 200)
}

df = pd.DataFrame(data)

# Create customized jointplot
jg = sns.jointplot(data=df, x="score1", y="score2", hue="class",
                   kind="scatter", height=8, 
                   joint_kws={'alpha': 0.6, 's': 40},
                   marginal_kws={'alpha': 0.7})

# Add title
jg.fig.suptitle('Student Scores by Subject', y=1.02)
plt.show()

Key Parameters

Parameter Description Example
hue Column name for color coding hue="category"
height Size of the figure height=6
alpha Point transparency (via joint_kws) joint_kws={'alpha': 0.6}
kind Type of plot kind="scatter"

Conclusion

The hue parameter in sns.jointplot() enables effective visualization of relationships between three variables. Use categorical data for the hue parameter to create distinct color groups that enhance data interpretation.

Updated on: 2026-03-25T23:07:24+05:30

442 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements