Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Science

DataFrame.plot.scatter() | How to Scatter Plot in Pandas?

Piyush Kaushal by Piyush Kaushal
December 17, 2023
Reading Time: 6 mins read
How to make scatter plot in pandas
Follow us on Google News   Subscribe to our newsletter

Pandas library in Python provides a lot of tools required for our analysis. In data analysis and visualization, a scatter plot is a powerful tool to understand the relationship between two numbers or variables. A scatter plot allows us to represent each data point and identify patterns, correlations, and outliers in the data. In this article, we will explore how to create a scatter plot using the popular Python library, Pandas. 

What is the plot.scatter() in Pandas?

A scatter plot is a method to display data that shows the relationship between two numerical variables. Each dot on the scatter plot can represent a single data point, with the x-axis representing one variable and the y-axis representing the other variable. By plotting the data points on a graph, we can visually analyse the relationship between the two variables.

The plot.scatter() method in Pandas allows us to make scatter for our data visualisation needs.

Following is the syntax of the plot.scatter():

DataFrame.plot.scatter(x, y, s=None, c=None, **kwargs)

Parameters:

  • x: The column name or position to be used as the horizontal coordinates for each point.
  • y: The column name or position to be used as the vertical coordinates for each point.
  • s: The size of each point. It can be a single scalar, a string with the name of the column to be used for marker size, or a sequence of scalars.
  • c: The colour of each point. It can be a single colour string, a sequence of colour strings, or a column name or position whose values will be used to colour the marker points according to a colourmap.
  • **kwargs: Additional keyword arguments to pass on to the underlying plotting function.

Now, let us look at how to use the method to draw scatter plots of our data.

Creating a Scatter Plot from a DataFrame

We can simply use a DataFrame and then apply plot.scatter. To create a scatter plot from the DataFrame, we can use the plot.scatter() function and specify the ‘x’ and ‘y’ columns.

Let us look at an example:

import pandas as pd

# Create DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})

# Display the DataFrame
print('Original DataFrame:\n', df)

# Create scatter plot
df.plot.scatter(x='A', y='B')

Output:

Original DataFrame:
    A   B
0  1   2
1  2   4
2  3   6
3  4   8
4  5  10
Creating a Scatter Plot from a DataFrame

Customising our Scatter Plot

We can also customise our scatter to meet our needs. Pandas provide various options to customize the appearance of the scatter plot. We can modify the size, colour, and other properties of the data points to improve the visualization.

Let us first see how to modify the size of the dots. To provide customised sizes for each dot, we can pass a sequence of scalars with the sizes. A sequence of scalars is similar to a list of integers.

See the Python code below:

import pandas as pd

# Create DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})

# Display the DataFrame
print('Original DataFrame:\n', df)

# Create scatter plot with customized size
df.plot.scatter(x='A', y='B', s=[50, 100, 150, 200, 250])

Output:

Original DataFrame:
    A   B
0  1   2
1  2   4
2  3   6
3  4   8
4  5  10
Customising our Scatter Plot

In the above example, we assigned different sizes to every dot based on a sequence of scalars [50, 100, 150, 200, 250]. The first dot will have a size of 50, the second dot will have a size of 100, and so on.

Now, let us learn how to change the colour of the dots:

import pandas as pd

# Create DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})

# Display the DataFrame
print('Original DataFrame:\n', df)

# Create scatter plot with customized color
df.plot.scatter(x='A', y='B', c='red')

Output:

Original DataFrame:
    A   B
0  1   2
1  2   4
2  3   6
3  4   8
4  5  10
Changing color of dotes

We can define various colours in the ‘c’ parameter of the plot.scatter method. In the example, we defined our scatter points to be red.

Using Matplotlib to Create Scatter Plots

We can also use the traditional library for plotting graphs, matplotlib to plot scatter plots.

Matplotlib is a powerful library for data visualization and provides extensive customization options.

Let us look at an example of how to draw a scatter plot using matplotlib:

import matplotlib.pyplot as plt
import pandas as pd

# Create DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})

# Display the DataFrame
print('Original DataFrame:\n', df)

# Create scatter plot using Matplotlib
plt.scatter(df['A'], df['B'])
plt.xlabel('A')
plt.ylabel('B')
plt.title('Scatter Plot')
plt.show()

Output:

Original DataFrame:
    A   B
0  1   2
1  2   4
2  3   6
3  4   8
4  5  10
Create Scatter Plots using Matplotlib

You can learn about other Python libraries for Data Science as well.

Conclusion

In this article, we learned how to create scatter plots using the Pandas library in Python. Scatter plots are a powerful tool for seeing the relationship between two numerical variables in a DataFrame. We learned about the syntax and parameters of the plot.scatter() function and saw examples of creating basic scatter plots as well as customizing the size and colour of the dots.

ShareTweetShareSendSend
Piyush Kaushal

Piyush Kaushal

I am Piyush Kaushal, currently pursuing a degree in software engineering at a prestigious government university. I am dedicated to staying informed about the latest technological developments and continuously expanding my knowledge base. I take great pleasure in sharing my expertise in data science and coding with fellow aspiring minds.

RelatedPosts

Moving Average in Pandas

Calculate Moving Average in Pandas (with code)

January 12, 2024
Pandas Convert Datetime to Date Column

Convert Datetime to Date Column in Pandas (with code)

January 4, 2024
Convert Pandas DataFrame to NumPy Array

Convert Pandas DataFrame to NumPy Array (with code)

January 3, 2024
Pandas DataFrame isna() Method

Pandas DataFrame isna() Method Explained

January 3, 2024
Pandas DataFrame copy() Method

Pandas DataFrame copy() Method Explained

January 1, 2024

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.