In this tutorial, we’ll look at how to create a scatter plot from columns of a pandas dataframe.

## Scatter Plot in Pandas

To create a scatter plot from dataframe columns, use the pandas dataframe `plot.scatter()`

function. The following is the syntax:

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

ax = df.plot.scatter(x, y)

Here, x is the column name or column position of the coordinates for the horizontal axis and y is the column name or column position for coordinates of the vertical axis.

Under the hood, the `df.plot.scatter()`

function creates a matplotlib scatter plot and returns it. You can also use the matplotlib library to create scatter plots by passing the dataframe column values as input.

## Examples

Let’s look at some examples of plotting a scatter directly from pandas dataframes. First, let’s create a dataframe that we’ll be using throughout this tutorial.

**Upskill your career right now →**

import pandas as pd # dataframe of height and weight football players df = pd.DataFrame({ 'Height': [167, 175, 170, 186, 190, 188, 158, 169, 183, 180], 'Weight': [65, 70, 72, 80, 86, 94, 50, 58, 78, 85], 'Team': ['A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'B', 'A'] }) # display the dataframe print(df)

Output:

Height Weight Team 0 167 65 A 1 175 70 A 2 170 72 B 3 186 80 B 4 190 86 B 5 188 94 B 6 158 50 A 7 169 58 A 8 183 78 B 9 180 85 A

The above dataframe contains the height (in cm) and weight (in kg) data of football players from two teams, A and B.

### 1. Scatter plot of column values

Let’s create a scatter plot of the “Height” column vs the “Weight” column of the dataframe.

ax = df.plot.scatter(x="Weight", y="Height")

Output:

The above plot shows the relation between height and weight of football players from the dataframe. You can see that there’s a positive correlation between the two.

### 2. Scatter plot with different color for each category

Let’s color each of the data points in the scatter plot to reflect the team of each player. First, we’ll add an additional column to the above dataframe to depict the color to be used for each data point.

# add additional column for color representing each teach df['Team Color'] = df['Team'].map({'A': 'Red', 'B': 'Blue'}) # display the dataframe print(df)

Output:

**Upskill your career right now →**

Height Weight Team Team Color 0 167 65 A Red 1 175 70 A Red 2 170 72 B Blue 3 186 80 B Blue 4 190 86 B Blue 5 188 94 B Blue 6 158 50 A Red 7 169 58 A Red 8 183 78 B Blue 9 180 85 A Red

We use the color “Red” to represent the players from team A and “Blue” to represent players from team B. Now, let’s plot the same scatter plot but this time with colored datapoints representing their respective teams.

ax = df.plot.scatter(x="Weight", y="Height", c="Team Color")

Output:

We used the parameter `c`

to pass the column with the color of the data points to the `df.plot.scatter()`

function.

You can see that the data points from team A are colored red and those from team B are colored blue. An interesting observation from the above plot can be that the players from team A comparatively have a lower height and weight compared to that of team B.

For more on the scatter plot function in pandas, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**