Scatter plot from pandas dataframe

Create a Scatter Plot from Pandas DataFrame

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

To create a scatter plot from dataframe columns, use the pandas dataframe plot.scatter() function. The following is the syntax:

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.

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.

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


   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.

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

📚 Data Science Programs By Skill Level


Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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="Weight", y="Height")


Resulting scatter plot of Height and Weight columns of dataframe df

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.

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


   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")


Scatter plot of height and weight with red and blue color representing the team category

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.


  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top