Black Lives Matter. Please consider donating to Black Girls Code today.

Scatter Plots in Python

How to make scatter plots in Python with Plotly.

If you're using Dash Enterprise's Data Science Workspaces, you can copy/paste any of these cells into a Workspace Jupyter notebook.
Alternatively, download this entire tutorial as a Jupyter notebook and import it into your Workspace.
Find out if your company is using Dash Enterprise.

Install Dash Enterprise on Azure | Install Dash Enterprise on AWS

New to Plotly?

Plotly is a free and open-source graphing library for Python. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Scatter plot with Plotly Express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

With px.scatter, each data point is represented as a marker point, whose location is given by the x and y columns.

In [1]:
# x and y given as array_like objects
import as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
In [2]:
# x and y given as DataFrame columns
import as px
df = # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")

Set size and color with column names

Note that color and size data are added to hover information. You can add other columns to hover data with the hover_data argument of px.scatter.

In [3]:
import as px
df =
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                 size='petal_length', hover_data=['petal_width'])

Scatter plot in Dash

Dash is the best way to build analytical apps in Python using Plotly figures. To run the app below, run pip install dash, click "Download" to get the code and run python

Get started with the official Dash docs and learn how to effortlessly style & deploy apps like this with Dash Enterprise.


Line plot with Plotly Express

In [5]:
import as px
import numpy as np

t = np.linspace(0, 2*np.pi, 100)

fig = px.line(x=t, y=np.cos(t), labels={'x':'t', 'y':'cos(t)'})
In [6]:
import as px
df ="continent == 'Oceania'")
fig = px.line(df, x='year', y='lifeExp', color='country')

Scatter and line plot with go.Scatter

If Plotly Express does not provide a good starting point, it is possible to use the more generic go.Scatter class from plotly.graph_objects. Whereas has two functions scatter and line, go.Scatter can be used both for plotting points (makers) or lines, depending on the value of mode. The different options of go.Scatter are documented in its reference page.

Simple Scatter Plot

In [7]:
import plotly.graph_objects as go
import numpy as np

N = 1000
t = np.linspace(0, 10, 100)
y = np.sin(t)

fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))

Line and Scatter Plots

Use mode argument to choose between markers, lines, or a combination of both. For more options about line plots, see also the line charts notebook and the filled area plots notebook.

In [8]:
import plotly.graph_objects as go

# Create random data with numpy
import numpy as np

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5

fig = go.Figure()

# Add traces
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
fig.add_trace(go.Scatter(x=random_x, y=random_y2,

Bubble Scatter Plots

In bubble charts, a third dimension of the data is shown through the size of markers. For more examples, see the bubble chart notebook

In [9]:
import plotly.graph_objects as go

fig = go.Figure(data=go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13],
    marker=dict(size=[40, 60, 80, 100],
                color=[0, 1, 2, 3])

Style Scatter Plots

In [10]:
import plotly.graph_objects as go
import numpy as np

t = np.linspace(0, 10, 100)

fig = go.Figure()

    x=t, y=np.sin(t),
    marker_color='rgba(152, 0, 0, .8)'

    x=t, y=np.cos(t),
    marker_color='rgba(255, 182, 193, .9)'

# Set options common to all traces with fig.update_traces
fig.update_traces(mode='markers', marker_line_width=2, marker_size=10)
fig.update_layout(title='Styled Scatter',
                  yaxis_zeroline=False, xaxis_zeroline=False)

Data Labels on Hover

In [11]:
import plotly.graph_objects as go
import pandas as pd

data= pd.read_csv("")

fig = go.Figure(data=go.Scatter(x=data['Postal'],
                                text=data['State'])) # hover text goes here

fig.update_layout(title='Population of USA States')

Scatter with a Color Dimension

In [12]:
import plotly.graph_objects as go
import numpy as np

fig = go.Figure(data=go.Scatter(
    y = np.random.randn(500),
        color=np.random.randn(500), #set color equal to a variable
        colorscale='Viridis', # one of plotly colorscales

Large Data Sets

Now in Plotly you can implement WebGL with Scattergl() in place of Scatter()
for increased speed, improved interactivity, and the ability to plot even more data!

In [13]:
import plotly.graph_objects as go
import numpy as np

N = 100000
fig = go.Figure(data=go.Scattergl(
    x = np.random.randn(N),
    y = np.random.randn(N),