11.8 C
London
Friday, May 16, 2025
HomePandas in PythonGeneral Functions in PythonHow to Make a Scatterplot From a Pandas DataFrame

How to Make a Scatterplot From a Pandas DataFrame

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are two ways to create a scatterplot using data from a pandas DataFrame:

1. Use pandas.DataFrame.plot.scatter

One way to create a scatterplot is to use the built-in pandas plot.scatter() function:

import pandas as pd

df.plot.scatter(x = 'x_column_name', y = 'y_columnn_name')

2. Use matplotlib.pyplot.scatter

Another way to create a scatterplot is to use the Matplotlib pyplot.scatter() function:

import matplotlib.pyplot as plt

plt.scatter(df.x, df.y)

This tutorial provides an example of how to use each of these methods.

Example 1: Use Pandas

The following code shows how to use the plot.scatter() function to create a simple scatterplot:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [1, 3, 3, 4, 5, 7, 9, 12, 13, 15],
                   'y': [5, 7, 9, 7, 6, 12, 14, 18, 15, 22]})

#create scatterplot
df.plot.scatter(x='x', y='y')

pandas scatter plot

Note that you can use the s and c arguments to modify the size and color of the points, respectively:

df.plot.scatter(x='x', y='y', s=60, c='green')

Scatterplot using a pandas DataFrame

Example 2: Use Matplotlib

The following code shows how to use the pyplot.scatter() function to create a scatterplot:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'x': [1, 3, 3, 4, 5, 7, 9, 12, 13, 15],
                   'y': [5, 7, 9, 7, 6, 12, 14, 18, 15, 22]})

#create scatterplot
plt.scatter(df.x, df.y)

Note that you can use the s and c arguments to modify the size and color of the points, respectively:

plt.scatter(df.x, df.y, s=60, c='purple')

You can find more Python tutorials here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories