Scatter plots are a cornerstone of data analysis, showing relationships between two continuous variables at a glance. When you add interactivity through Plotly, the scatter plot becomes a powerful exploration tool where viewers can hover over points to see details, zoom into areas of interest, and filter dynamically. Unlike static scatter plots, interactive Plotly visualizations transform a simple chart into a window into your dataset.
Creating A Basic Plotly Scatter Plot
Plotly Express makes creating scatter plots effortless through a high-level API. You pass a DataFrame and specify which columns map to the x and y axes, and Plotly handles the rest. The resulting plot includes default hover labels, a toolbar for zooming and panning, and a legend if your data has categorical variables.
import plotly.express as px
import pandas as pd
# Load sample data
df = px.data.iris()
# Create basic scatter plot
fig = px.scatter(df, x='sepal_width', y='sepal_length')
fig.show()
With just three lines of code, you have an interactive scatter plot. Users can hover over any point to see the exact coordinates, click and drag to zoom, and use the toolbar to pan or reset the view. The interactivity is built in without any extra configuration.
Adding Color And Size Encoding
A scatter plot becomes richer when you add a third or fourth dimension through color and point size. Plotly’s color parameter lets you encode a categorical or continuous variable through hue, and the size parameter scales points according to a numeric variable. These visual channels reveal multivariate patterns that would be invisible in a 2D scatter plot.
import plotly.express as px
df = px.data.iris()
# Scatter plot with color and size encoding
fig = px.scatter(
df,
x='sepal_width',
y='sepal_length',
color='species',
size='petal_width',
hover_data=['petal_length'],
title='Iris Measurements by Species',
labels={
'sepal_width': 'Sepal Width (cm)',
'sepal_length': 'Sepal Length (cm)',
'petal_width': 'Petal Width (cm)'
}
)
fig.update_traces(marker=dict(opacity=0.7))
fig.show()
The color parameter maps species to different colors, instantly showing that Setosa, Versicolor, and Virginica form distinct clusters. The size parameter scales each point by petal width, so larger points have wider petals. The hover_data parameter includes additional columns in the hover tooltip, enriching the interactivity. Setting opacity=0.7 helps reveal overlapping points in dense regions.
Customizing Hover Information
By default, Plotly shows the coordinates of each point in the hover tooltip. You can customize this to show any columns you want, format numbers, and hide unnecessary information. This makes the tooltip a mini-dashboard where viewers see exactly the metrics they care about.
fig = px.scatter(
df,
x='sepal_width',
y='sepal_length',
color='species',
hover_data={
'sepal_width': ':.2f',
'sepal_length': ':.2f',
'petal_length': ':.2f',
'petal_width': ':.2f'
}
)
fig.update_traces(
hovertemplate='Species: %{customdata[4]}
' +
'Sepal Width: %{x:.2f} cm
' +
'Sepal Length: %{y:.2f} cm
' +
' '
)
fig.show()
The hover_data dictionary controls which columns appear in the tooltip and how they are formatted. The hovertemplate parameter gives you full control over the HTML structure of the tooltip, letting you create multi-line, richly formatted labels. Setting
Using Faceting For Multiple Views
When you have many categories, a single colored scatter plot becomes cluttered. Plotly’s faceting feature creates small multiples, a separate subplot for each category, so you can compare patterns across groups without visual interference.
fig = px.scatter(
df,
x='sepal_width',
y='sepal_length',
color='species',
facet_col='species',
facet_col_wrap=2,
title='Sepal Measurements by Species',
height=600,
width=900
)
fig.show()
With facet_col=’species’, Plotly creates one subplot per species. The facet_col_wrap=2 parameter arranges them in a grid of two columns. This layout makes it easy to compare the distribution of points within each species and spot any anomalies or special patterns. The shared axis ranges maintain consistency across facets, supporting fair comparison.
Exporting And Sharing Interactive Visualizations
One of Plotly’s greatest strengths is that interactive plots export to self-contained HTML files that work anywhere without a server. This makes sharing visualizations with colleagues effortless: you send a single HTML file and they can open it in any browser and explore.
fig.write_html('scatter_plot.html', config={'responsive': True})
The write_html function exports the figure as a complete, self-contained HTML document. The config={‘responsive’: True} parameter makes the plot adapt to different screen sizes, so it looks good on desktops, tablets, and phones. You can embed this HTML directly in a website, email it as an attachment, or link to it from a data portal. The interactivity persists across all platforms.