0
0
Pandasdata~5 mins

Scatter plots in Pandas

Choose your learning style9 modes available
Introduction

Scatter plots help us see how two sets of numbers relate to each other. They show patterns or trends in data points.

To check if two things increase or decrease together, like hours studied and test scores.
To find groups or clusters in data, like types of flowers based on petal size and length.
To spot outliers that don’t fit the usual pattern, like a very tall person in a group of average height.
To visualize relationships before building a prediction model.
To compare two measurements from an experiment or survey.
Syntax
Pandas
df.plot.scatter(x='column1', y='column2', color='color_name')

df is your data table (DataFrame).

x and y are the names of the columns you want to plot.

Examples
Plot age on the x-axis and height on the y-axis.
Pandas
df.plot.scatter(x='age', y='height')
Plot hours studied vs test score with red dots.
Pandas
df.plot.scatter(x='hours_studied', y='test_score', color='red')
Plot weight and blood pressure with green dots.
Pandas
df.plot.scatter(x='weight', y='blood_pressure', color='green')
Sample Program

This code creates a small table with hours studied and test scores. Then it draws a scatter plot to show how test scores change with hours studied.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
data = {'hours_studied': [1, 2, 3, 4, 5],
        'test_score': [50, 60, 65, 70, 80]}
df = pd.DataFrame(data)

# Make scatter plot
scatter = df.plot.scatter(x='hours_studied', y='test_score', color='blue')
plt.title('Hours Studied vs Test Score')
plt.xlabel('Hours Studied')
plt.ylabel('Test Score')
plt.show()
OutputSuccess
Important Notes

You need matplotlib installed to see the plot.

Scatter plots work best with numeric data.

You can add labels and titles to make the plot clearer.

Summary

Scatter plots show the relationship between two numeric columns.

Use df.plot.scatter() with x and y to create them.

Colors and labels help make the plot easier to understand.