Seaborn Scatter Plot in Python

Data visualization plays a pivotal role in information and deciphering complex datasets. Scatter plots are the best tool for visualising your data and the relationship between two numerical variables. When creating captivating and informative scatter plots in Python, Seaborn emerges as one of the top choices among data enthusiasts and analysts. In this weblog, we will explore the world of Seaborn scatter plot, learn how to create them, and discover some exciting options they offer.

How to Create a Seaborn Scatter Plot

Before we delve into Seaborn scatter plots’ intricacies, let’s ensure you have Seaborn installed. You can install it via pip if you haven’t already:

To get started, we need to import Seaborn and other necessary libraries into our Python script:

import seaborn as sns

import matplotlib.pyplot as plt

Next, we’ll load our dataset or generate sample facts for demonstration. For this weblog, we’ll use a fictional dataset containing information about the income and overall performance of various merchandise.

import pandas as pd

# Assuming you have a CSV file 'sales_data.csv'

data = pd.read_csv('sales_data.csv')

Once we have the dataset prepared, creating a basic Seaborn scatter plot is as simple as calling the `sns.Scatterplot()` characteristic and passing the statistics for the x and y axes:

# Basic Seaborn Scatter Plot

sns.scatterplot(data=data, x='product_price', y='units_sold')

plt.title('Scatter Plot of Product Price vs. Units Sold')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.show()

This code will display a scatter plot that shows how the product price relates to the number of units sold.

Seaborn Scatter Plot Options

Seaborn offers a plethora of alternatives to customize and enhance your scatter plots. Let’s explore some of the most normally used ones:

  • Hue

The `hue` parameter allows us to introduce a further categorical variable, resulting in exceptional colourations for exclusive classes. This is specifically useful whilst we need to visualize relationships among three variables.

sns.scatterplot(data=data, x='product_price', y='units_sold', hue='product_category')

plt.title('Scatter Plot of Product Price vs. Units Sold with Categories')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.legend(title='Product Category')

plt.show()
  • Size

The `size` parameter enables us to change the size of the markers based on a numerical variable. This helps emphasize specific data points that have higher importance.

sns.scatterplot(data=data, x='product_price', y='units_sold', size='customer_rating', sizes=(30, 200))

plt.title('Scatter Plot of Product Price vs. Units Sold with Customer Ratings')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.show()
  • Style

With the `style` parameter, you can assign different markers for each category in a categorical variable. This adds another layer of information to your scatter plot.

sns.scatterplot(data=data, x='product_price', y='units_sold', style='product_category')

plt.title('Scatter Plot of Product Price vs. Units Sold with Style')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.show()
  • Palette

The `palette` parameter allows you to choose a colour palette representing the different categories in the `hue` parameter. Seaborn offers a variety of visually appealing colour palettes.

sns.scatterplot(data=data, x='product_price', y='units_sold', hue='product_category', palette='Set2')

plt.title('Scatter Plot of Product Price vs. Units Sold with Custom Palette')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.legend(title='Product Category')

plt.show()

Seaborn Scatter Plot Examples

Let’s explore some real-world examples of Seaborn scatter plots that showcase their versatility and power in visualizing different types of data relationships:

1. Relationship Between Two Numerical Variables

In our first example, we will create a scatter plot for visual of the correlation between the temperature and the range of ice cream cones offered on a warm summer season day.

# Assuming we have the data for temperature and ice cream sales

sns.scatterplot(x='temperature', y='ice_cream_sales', data=data)

plt.title('Scatter Plot of Temperature vs. Ice Cream Sales')

plt.xlabel('Temperature')

plt.ylabel('Ice Cream Sales')

plt.show()

“`

2. Examining Outliers

Scatter plots are excellent for identifying outliers in a dataset. In this example, we’ll use a scatter plot to identify any anomalies in the distribution of students’ test scores.

# Assuming we have the data for test scores

sns.scatterplot(x='student_id', y='test_score', data=data)

plt.title('Scatter Plot of Student ID vs. Test Scores')

plt.xlabel('Student ID')

plt.ylabel('Test Score')

plt.show()

3. Visualizing Clusters

Seaborn scatter plots can help visualize the presence of clusters in data. Let’s create a scatter plot to examine the clustering of data points in a customer segmentation analysis.

# Assuming we have the data for customer segments

sns.scatterplot(x='annual_income', y='spending_score', hue='segment', data=data)

plt.title('Scatter Plot of Annual Income vs. Spending Score with Clusters')

plt.xlabel('Annual Income')

plt.ylabel('Spending Score')

plt.legend(title='Segment')

plt.show()

Seaborn 3D Scatter Plots

In some scenarios, you might have three numerical variables that you want to visualize simultaneously. Seaborn also offers 3D scatter plots for this purpose. To create a 3D scatter plot, we need to use the `mpl_toolkits` library in addition to Seaborn.

from mpl_toolkits.mplot3d import Axes3D

# Assuming we have the data for three variables: x, y, and z

fig = plt.figure(figsize=(8, 6))

ax = fig.add_subplot(111, projection='3d')

ax.scatter(data['x'], data['y'], data['z'], c='blue', marker='o')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.title('3D Scatter Plot')

plt.show()

Seaborn Scatter Plots with Regression Lines

Regression lines visualize the relationship between two numerical variables and help identify trends or correlations in the data. Seaborn makes it simple to add regression lines to scatter plots using the `sns.regplot()` function.

sns.regplot(data=data, x='product_price', y='units_sold')

plt.title('Scatter Plot with Regression Line')

plt.xlabel('Product Price')

plt.ylabel('Units Sold')

plt.show()

Conclusion

Seaborn scatter plots are indispensable for exploring data relationships and gaining insights into your datasets. They provide an easy and aesthetically pleasing way to visualize numerical variables and discover patterns, trends, and outliers. With various customization options

 You can tailor your scatter plots to suit specific data visualization needs.

Throughout this blog post, we learned how to create basic and advanced Seaborn scatter plots, explored the options to enhance their appearance, and delved into real-world examples. filled with this knowledge, you are now equipped to harness the full potential of Seaborn scatter plots in your data exploration and analysis endeavours.

Happy plotting!

(Note: The code snippets provided in this blog post assume the existence of appropriate datasets for visualization. Please replace the placeholder data with your actual data when implementing these examples in your projects.)

For more Related Topics

Stay in the Loop

Receive the daily email from Techlitistic and transform your knowledge and experience into an enjoyable one. To remain well-informed, we recommend subscribing to our mailing list, which is free of charge.

Latest stories

You might also like...