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.
Key Points –
- Use the
.plot()
method with the kind parameter set to"scatter"
for scatter plots. - Provide the
x
andy
parameters to specify the columns for the x-axis and y-axis data. - Control the marker style, size, and transparency using parameters like
s
,c
, andalpha
. - Use the
c
parameter to color points by a specific column or value. - Use the
xlabel
andylabel
arguments or set axis labels directly using Matplotlib methods. - Ensure the data is clean and contains no missing values in the
x
andy
columns.
Quick Examples of Scatter Plot
If you are in a hurry below are some quick examples of how to create scatter plot chart.
# Quick examples of scatter plot
# 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')
Syntax of Pandas plot.scatter()
# Syntax of plot.scatter()
DataFrame.plot.scatter(x, y, s = none, c = none)
Parameters of the plot.scatter()
Below are the parameters of the scatter() function.
x
: column name to be used as horizontal coordinates for each pointy
: column name to be used as vertical coordinates for each points
: size of dotsc
: color of dots
Return Value
It returns a scatter plot.
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.
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.
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.
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.
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.
Related Articles
- How to add title to Pandas plots?
- How to change Plot size in pandas?
- How to generate line plot in Pandas?
- How to add legends to plots in Pandas
- Pandas Series Tutorial with Examples
- How to generate histograms in Pandas?
- How to Plot the Boxplot from DataFrame?
- How to Plot Columns of Pandas DataFrame
- How to Generate Time Series Plot in Pandas?
- How to Change Position of a Column in Pandas
- How to distribute column values in Pandas plot?
- Create Pandas Plot Bar Explained with Example