How to Create a Scatter Plot in Matplotlib : 3 Steps Only

Scatter Plot allows you to compare and find the relationship between the two variables. Using it you can find the correlation between the plotted variables. You will come to know that many machine learning or deep learning models are made before checking the correlation between the variables. It helps you to reduce the features from your training dataset. In this entire tutorial, you will learn how to create a scatter plot in matplotlib with steps.

Steps to Create a Scatter Plot in Matplotlib

Please note that I am using Jupyter notebook for implementing Matplotlib Scatter Example. So it’s best that you should also code there for more understanding.

Step 1: Import all the necessary libraries

The first step is to import matplotlib, NumPy, and other required libraries for our tutorial. Let’s import them using the import statement.

``````import matplotlib.pyplot as plt
import numpy as np
import pandas as pd``````

For plotting Scatter plot in Matplotlib you have to first create two variables with data points Let’s say x and y. Here I am reading the EURUSD forex exchange market dataset that is CSV format. Then I will extract the open and close as the x and the y variable. As the dataset is in a CSV file, so to read the dataset I will use the Pandas module and will use the pd.read_csv() method. Execute the code below lines of code.

``````import pandas as pd
x = data["Open"]
y = data["Close"]``````

Step 3: Create a scatter plot in matplotlib

After reading the dataset you can now plot the scatter plot using the plt.scatter() method. The common syntax of the plt.scatter() is below.

``matplotlib.pyplot.scatter(x, y, marker=None)``

Here x and y are the two variables you want to find the relationship and marker is the marker style of the data points. You can explore it from Matplotlib Maker Style Documentation. In our plot marker style is a circle(o). If you want to explore more parameters then you can read the official Matplotlib Scatter Documentation.

Execute the lines of code below to plot the scatter chart.

``````%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
import pandas as pd
x = data["Open"]
y = data["Close"]
plt.figure(figsize=(9,6))
plt.scatter(x,y,marker="o");``````

Please note that Here the plt.figure() is used to change the size of the plot. The %matplotlib inline is used to show the figure inline.

Output

You can see in the above plot the open and close are linearly dependent. Seein this you can confirm that both Open and Close are positively correlated.

Conclusion

Congrats you have successfully plot the scatter figure. These are the steps to create a scatter plot in matplotlib. Try to implement these steps on your dataset to find the relationship between the variables. And If you are facing any difficulties then contact us for more help.

Dataset