How to create a Scatter plot with Python?

In this Data Visualization tutorial we’ll learn how to quickly render and customize custom charts using Python and the Pandas library. It’s important to note that the Pandas plotting capabilities are a subset from those available in Matplotlib, a powerful Data Visualization library, which we have covered in other tutorials.

Python scatter plots example – a step-by-step guide

Importing libraries

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

Creating data for the Chart

We’ll define the x and y variables as well as create a DataFrame.

# define x and y
x = np.random.poisson(80,200)
y= np.random.poisson(32,200)

# Wrap x,y values into a DataFrame
my_data = pd.DataFrame.from_dict({'Duration':x, 'Cost':y})

Drawing a chart with Pandas

Once we have our DataFrame, we can invoke the DataFrame.plot() method to render the scatter using the built-in plotting capabilities of Pandas.

my_data.plot.scatter(x='Duration', y='Cost', title= 'Simple scatter with Pandas');

Here’s our chart:

Changing the plot colors

We can easily change the color of our scatter points.

# color change
my_data.plot.scatter(x='Duration', y='Cost', title= 'Simple scatter with Pandas', c='green');

Displaying the scatter legend in Pandas

We used the label parameter to define the legend text. Note the usage of the bbox_to_anchor parameter to offset the legend from the chart.

my_data.plot.scatter(x='Duration', y='Cost', title= 'Simple scatter with Pandas', label= ['Trip duration', 'Trip Cost']).legend(bbox_to_anchor= (1.02, 1));

Rendering a Plot with Matplotlib

Matplotlib offers a rich set of capabilities to create static charts. Here’s a simple example/

import matplotlib.pyplot as plt
plt.title('Simple scatter with Matplotlib');

Change the marker type and size

We can easily modify the marker style and size of our plots.

# marker style and size
plt.scatter(x,y, marker = 'x', s=70 );
plt.title('Scatter example with custom markers');

Adding a legend to the chart

You are able to display the legend quite easily using the following command:


Scatter plot in Python with Seaborn

For completeness, we are including a simple example that leverages the Seaborn library (also built on Matplotlib).

Note that you will need to ensure that the Seaborn library is installed as part of your Python development environment before using it in Jupyter or other Python IDE.

import seaborn as sns

x = my_data['Duration']
y = my_data['Cost']
ax = sns.scatterplot(x=x, y=y)
ax.set_title('Scatter example')

Here’s our chart: