Plotly Express scatter plots: set categorical color and hover details, then save as HTML
Plotly Express `px.scatter` creates one point per data row and maps columns to roles through arguments such as `x`, `y`, and `color`. Passing a categorical column to `color` separates groups by color and adds legend entries; the returned Figure can then be customized. This guide builds a scatter plot from a fictional campaign table, checks its hover details, and saves it as interactive HTML. Official Plotly Python documentation was checked on 2026-11-18.
Plotly Express scatter plots: set categorical color and hover details, then save as HTML
Plotly Express px.scatter creates one point per data row and maps columns to roles through arguments such as x, y, and color. Passing a categorical column to color separates groups by color and adds legend entries; the returned Figure can then be customized. This guide builds a scatter plot from a fictional campaign table, checks its hover details, and saves it as interactive HTML. Official Plotly Python documentation was checked on 2026-11-18.
When should you use Plotly Express?
Plotly Express is Plotly's high-level API for making common charts—scatter plots, lines, bars, histograms—with little code. A call such as px.scatter(...) creates a Figure, returned as a plotly.graph_objects.Figure instance. You can continue editing it with Figure methods such as update_layout, update_traces, and add_trace. Graph Objects provides more low-level control when you need to construct individual coordinates and trace properties, while Express is concise for typical charts.
A scatter plot usually maps one row to one point. Numeric x and y columns let you examine how two values vary together. Color can distinguish groups, but an observed association does not establish cause and effect. The values below are made up to demonstrate the API and support no statistical conclusion.
Install and run the example
In a Python environment with pip, install Plotly's Express dependencies and pandas, as shown in the official getting-started guide:
python -m pip install "plotly[express]" pandasSave the following as plotly_scatter.py and run it. Do not name your script plotly.py: a file with the library's name can shadow the package during import.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"campaign": ["A-01", "A-02", "B-01", "B-02", "C-01", "C-02"],
"ad_spend": [2, 4, 3, 6, 5, 8],
"sales": [18, 25, 20, 31, 26, 38],
"channel": ["Search", "Search", "Social", "Social", "Email", "Email"],
"region": ["North", "South", "North", "South", "North", "South"]
})
fig = px.scatter(
df,
x="ad_spend",
y="sales",
color="channel",
hover_name="campaign",
hover_data=["region"],
labels={
"ad_spend": "Ad spend (example units)",
"sales": "Sales (example units)",
"channel": "Campaign channel",
"region": "Region"
},
title="Example campaigns by channel: ad spend and sales"
)
fig.update_traces(marker_size=12, opacity=0.8)
fig.update_layout(legend_title_text="Campaign channel")
fig.write_html("channel_scatter.html")Run python plotly_scatter.py. The current working directory will contain channel_scatter.html. By default, write_html embeds the Plotly.js library, creating a standalone interactive file that can be opened in a browser; it may be larger than 5 MB. The code was reviewed against the API docs but was not executed in a Python environment during drafting.
How the arguments map to the chart
| Argument | What it does here | Check |
|---|---|---|
x, y | Map advertising spend and sales to point position | Are both columns numeric and aligned to the same campaign rows? |
color="channel" | Assign distinct colors and legend categories to channel strings | Are Search, Social, and Email groups rather than continuous measurements? |
hover_name | Shows a campaign name as the hover title | Does the name identify the row clearly? |
hover_data | Adds region as supporting detail | Include only needed fields; omit sensitive columns |
labels |
Plotly Express includes plotted values such as x, y and color in hover labels by default. hover_name becomes the bold title, while hover_data adds a column such as region. A categorical channel creates separate colors and legend groups. A numeric color column may instead use a continuous color scale. If a code is stored as a number, first decide whether it is a category identifier or a measured quantity.
Check the browser result and troubleshoot
Open the HTML file and hover over points. Check that the tooltip shows the campaign, x/y values, channel, and region. Click legend entries to hide or show a channel, then try dragging to zoom or pan. Verify that each of the six points corresponds to the row's spend and sales, all three channel labels appear, and a tooltip does not show another campaign's region.
**`ModuleNotFoundError`:** Install packages in the same Python environment that runs the script with `python -m pip install "plotly[express]" pandas`. Match the interpreter paths if you use multiple environments.
**Axis titles still show column names:** Confirm `labels` keys exactly match the DataFrame columns: `ad_spend`, `sales`, `channel`, and `region`.
**The legend does not split into the intended groups:** Check the column passed to `color`. Numeric values can be treated as continuous and use a color scale.
**Points overlap and are hard to inspect:** Confirm x and y come from the same rows; then adjust marker opacity or size. `hover_name` can help identify points in a small dataset.
**The HTML file opens but interaction is unavailable:** Open the file in a browser and keep the default self-contained export. If you change `include_plotlyjs` to `"cdn"`, internet access is needed to load Plotly.js.
Advertising spend and sales here are synthetic; they are not evidence of a causal relationship. With real data, confirm that regions, periods, currencies and units are comparable before plotting, and check whether group size or overplotting hides important differences.
Sources
[Plotly Documentation: Getting started with Plotly in Python](https://plotly.com/python/getting-started/)
[Plotly Documentation: Plotly Express in Python](https://plotly.com/python/plotly-express/)
[Plotly Documentation: Scatter plots in Python](https://plotly.com/python/line-and-scatter/)
[Plotly Documentation: Hover text and formatting](https://plotly.com/python/hover-text-and-formatting/)
[Plotly Documentation: Interactive HTML export](https://plotly.com/python/interactive-html-export/)
[Plotly Documentation: Graph objects in Python](https://plotly.com/python/graph-objects/)