Table of Contents
A scatterplot is a visual representation of the relationship between two numerical variables. To create a scatterplot from a Pandas DataFrame, you can use the “plot.scatter()” function. This function takes in the two columns from the DataFrame that you want to plot and generates a scatterplot with those variables as the x and y axes. By specifying the desired columns and adding any necessary labels and titles, you can easily create a scatterplot from a Pandas DataFrame to visualize the relationship between your data.
Make a Scatterplot From a Pandas DataFrame
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 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 function:
import matplotlib.pyplotas 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 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')

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

Example 2: Use Matplotlib
The following code shows how to use the function to create a scatterplot:
import pandas as pd import matplotlib.pyplotas 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 .
Cite this article
stats writer (2024). How can I create a scatterplot from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-from-a-pandas-dataframe/
stats writer. "How can I create a scatterplot from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 24 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-from-a-pandas-dataframe/.
stats writer. "How can I create a scatterplot from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-from-a-pandas-dataframe/.
stats writer (2024) 'How can I create a scatterplot from a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-scatterplot-from-a-pandas-dataframe/.
[1] stats writer, "How can I create a scatterplot from a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.
stats writer. How can I create a scatterplot from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
