• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:9 mins read
You are currently viewing How to Plot a Scatter Plot Using Pandas?

Pandas DataFrame plot.scatter() is used to create a scatter plot by using dots to represent values of two different numeric variables. A Scatter plot is a type of data visualization technique that shows the relationship between two numerical variables. Each dot on a scatter plot represents an individual data point. In this article, I will explain the concept of scatter plots by using the plot() function and creating scatter plot from the DataFrame.

Advertisements

1. Quick Examples of Scatter Plot

If you are in a hurry below are some quick examples of how to create scatter plot chart.


# Below are the quick examples

# Example 1: Create scatter plot
df.plot.scatter(x='x', y='y')

# Example 2: organize the scatter plot
df.plot.scatter(x='x', y='y', s = 100, c='purple')

# Example 3: create scatterplot
plot.scatter(df.x, df.y)

# Example 4: Customize the scatter plot
plot.scatter(df.x, df.y, s=60, c='purple')

2. Syntax of Pandas plot.scatter()


# Syntax of plot.scatter()
DataFrame.plot.scatter(x, y, s = none, c = none)

2.1 Parameters of the plot.scatter()

Below are the parameters of the scatter() function.

  • x: column name to be used as horizontal coordinates for each point
  • y: column name to be used as vertical coordinates for each point
  • s: size of dots
  • c: color of dots

2.2 Return Value

It returns a scatter plot.

3. Create Scatter Plot from Pandas DataFrame

In Pandas Scatter plot is one of the visualization techniques to represent the data from a DataFrame. We can use the plot.scatter() function to create a simple scatterplot. We can also create scatter plot from plot() function and this can also be used to create bar graph, plot box, histogram and plot bar in Pandas.

Let’s create Pandas DataFrame from Python Dicttionary.


import pandas as pd
# Create DataFrame
df = pd.DataFrame({'x': [5, 10, 15, 20, 25, 30, 35],
                   'y': [5, 10, 15, 20, 25, 30, 35]})
print(df)

Yields below output.


# Output:
    x   y
0   5   5
1  10  10
2  15  15
3  20  20
4  25  25
5  30  30
6  35  35

Let’s create a scatter plot using data from the DataFrame.


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

Yields below output.

Pandas scatter plot
Scatter plot using Pandas

3.1. Customize the Scatter Plot

We can customize the scatter plot using the ‘s‘ and ‘c‘ arguments to modify the size and color of the points, respectively. Use param c to specify the color of the dot.


# Organize the scatter plot
df.plot.scatter(x='x', y='y', s = 100, c='purple')

Yields below output.

Scatter plot using Pandas
Scatter plot using Pandas

4. Use Matplotlib to Create Scatter Plot

Matplotlib is another most used library in Python that is used to visualize the data in a charts. It provides the scatter() function to create the scatter plots. Use the pyplot.scatter() function to create a scatter plot, in order to use it you have to import is by using import matplotlib.pyplot.


import matplotlib.pyplot as plot
# Create scatterplot
plot.scatter(df.x, df.y)

Yields below output.

dataframe scatter plot
Scatter plot using Matplotlib

Customize the scatter plot by modifying the s and c parameters with desired values using the plot.scatter() function. Let’s customize.


# Customize the scatter plot
plot.scatter(df.x, df.y, s=60, c='purple')

Yields below output.

Scatter plot Pandas
Scatter plot using Matplotlib

5. Conclusion

In this article, I have explained the concept of scatter plot and using the scatter() function how we can plot the given DataFrame into a scatter plot. I also explained how to customize the scatter plot dots color and size with desired values and finally learned how to use the Matplotlib library to create a scatter plot.

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.