Creating a Joint Plot with Categorical Distinction

  • Share this:

Code introduction


This function uses seaborn's jointplot method to create a scatter plot and color-code different categories based on a given categorical variable. It is suitable for analyzing the relationship between two continuous variables and distinguishing them by color.


Technology Stack : Seaborn, Pandas, NumPy, Matplotlib

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import seaborn as sns
import numpy as np
import pandas as pd

def plot_distinct_categories(data, x, y, hue):
    """
    This function uses seaborn to create a jointplot with a distinct categories representation
    for a given categorical variable.
    """
    # Create a DataFrame
    df = pd.DataFrame(data)

    # Create the jointplot
    g = sns.jointplot(x=x, y=y, data=df, kind='scatter', hue=hue, sizes=(50, 100))

    # Display the plot
    sns.plt.show()

# Example usage:
# data = {
#     'Category': ['A', 'A', 'B', 'B', 'C', 'C'],
#     'Value': [1, 2, 3, 4, 5, 6],
#     'Color': ['Red', 'Red', 'Blue', 'Blue', 'Green', 'Green']
# }
# plot_distinct_categories(data, 'Value', 'Category', 'Color')