How to Create a Scatter Diagram in Python using Matplotlib

The following template can be used in order to create a scatter diagram in Python using Matplotlib:

import matplotlib.pyplot as plt

plt.scatter(xAxis,yAxis)
plt.title('title name')
plt.xlabel('xAxis name')
plt.ylabel('yAxis name')
plt.show()

In the next section, you’ll see the steps to create a scatter diagram using a practical example.

Steps to Create a Scatter Diagram in Python using Matplotlib

Step 1: Install the Matplotlib module

If you haven’t already done so, install the matplotlib module using the following command (under Windows):

pip install matplotlib

You may check this guide for the steps to install a module in Python using pip.

Step 2: Gather the data for the scatter diagram

Next, gather the data to be used for the scatter diagram.

For example, let’s say that you have the following data about an economy:

Unemployment_RateStock_Index_Price
6.11500
5.81520
5.71525
5.71523
5.81515
5.61540
5.51545
5.31560
5.21555
5.21565

The ultimate goal is to depict the relationship between the Unemployment_Rate and the Stock_Index_Price.

You can accomplish this goal using a scatter diagram.

Step 3: Capture the data in Python

You can capture the above dataset in Python using lists:

Unemployment_Rate = [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2]
Stock_Index_Price = [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]

print(Unemployment_Rate)
print(Stock_Index_Price)

If you run the above code in Python, you’ll get the following lists with the required information:

lists

Step 4: Create the scatter diagram in Python using Matplotlib

For this final step, you may use the template below in order to create the scatter diagram in Python:

import matplotlib.pyplot as plt

plt.scatter(xAxis,yAxis)
plt.title('title name')
plt.xlabel('xAxis name')
plt.ylabel('yAxis name')
plt.show()

For our example, the:

  • xAxis is the Unemployment_Rate
  • yAxis is the Stock_Index_Price
  • title name is the ‘Unemployment Rate Vs Stock Index Price’
  • xAxis name is the ‘Unemployment Rate’
  • yAxis name is the ‘Stock Index Price’

Putting everything together, here is the complete Python code to create the scatter diagram for our example:

import matplotlib.pyplot as plt
   
Unemployment_Rate = [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2]
Stock_Index_Price = [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
  
plt.scatter(Unemployment_Rate, Stock_Index_Price, color='green')
plt.title('Unemployment Rate Vs Stock Index Price', fontsize=14)
plt.xlabel('Unemployment Rate', fontsize=14)
plt.ylabel('Stock Index Price', fontsize=14)
plt.grid(True)
plt.show()

Run the code in Python, and you’ll get this scatter diagram:

How to Create a Scatter Diagram in Python using Matplotlib

You may notice that a negative relationship exists between the Unemployment_Rate and the Stock_Index_Price. This means that when the Unemployment Rate increases, the Stock Index Price decreases.

Scatter diagrams are especially useful when applying linear regression models. Those types of diagrams can help you determine if there is a linear relationship between the variables.

Optionally: Create the Scatter Diagram using Pandas DataFrame

So far, you have seen how to capture the dataset in Python using lists (step 3 above).

Alternatively, you may capture the data using Pandas DataFrame.

The result would be the same under both cases.

Here is the Python code that you may apply using Pandas DataFrame:

import pandas as pd
import matplotlib.pyplot as plt
   
data = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df = pd.DataFrame(data,columns=['Unemployment_Rate','Stock_Index_Price'])
  
plt.scatter(df['Unemployment_Rate'], df['Stock_Index_Price'], color='green')
plt.title('Unemployment Rate Vs Stock Index Price', fontsize=14)
plt.xlabel('Unemployment Rate', fontsize=14)
plt.ylabel('Stock Index Price', fontsize=14)
plt.grid(True)
plt.show()

As before, you’ll get the same relationship between the Unemployment Rate and the Stock Index Price:

Create a Scatter Diagram in Python using Matplotlib