The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →For separate categories, draw one scatter collection per category, assign each a label, then call ax.legend(). For values encoded by a single scatter plot’s colors or marker sizes, use that collection’s legend_elements() method and pass its handles and labels to the legend.
Choose the legend method that matches your scatter plot
Matplotlib legends describe plotted artists. The simplest approach depends on whether your markers represent distinct groups or numeric values mapped to color or size.
| What the markers represent | Recommended approach |
|---|---|
| Discrete groups, each with its own plotted collection | Use one scatter() call per group, set a descriptive label, then call ax.legend(). |
| Numeric values mapped to color in one collection | Keep the collection returned by scatter(); use legend_elements(prop="colors"). |
| Numeric values mapped to marker size in one collection | Keep the collection returned by scatter(); use legend_elements(prop="sizes"). |
| Both color and size encode values | Create separate, titled legends from the same collection, preserving the first legend with ax.add_artist(). |
Matplotlib’s stable scatter-with-legend gallery documents the separate-collection pattern. The examples and API references cited here are from Matplotlib’s stable documentation accessed October 4, 2026; the gallery, collections API, and figure API identify version 3.11.2, while the pyplot legend reference identifies 3.11.1. Stable documentation may change, so check the API if you are targeting a materially older release. Matplotlib scatter plot with a legend · Matplotlib collections API · Matplotlib pyplot legend reference.
Add a legend for discrete groups
Use a separate scatter collection for each group and give it a meaningful label. Matplotlib’s gallery recommends creating one scatter plot per item that should appear in the legend and setting its label accordingly.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →#1 Best Overall
fig, ax = plt.subplots()
for group, color in groups:
ax.scatter(group.x, group.y, color=color, label=group.name)
ax.legend(title="Group")
Here, groups represents your own iterable of group data and colors; group.name becomes the legend text. A title such as “Group” clarifies what the entries identify.
Make a legend for color values
If one scatter collection maps numeric values to color, retain the object returned by ax.scatter(). Its legend_elements() method returns matching legend handles and labels.
Rank #2
points = ax.scatter(x, y, c=values)
handles, labels = points.legend_elements(prop="colors")
ax.legend(handles, labels, title="Value")
For more control over generated entries or their appearance, legend_elements() accepts options such as num and fmt; a formatter can also shape the labels. These options help when you want to limit the entries or make their numeric text easier to read. See the collections API for the method’s parameters.
Make a legend for marker sizes
For numeric values mapped to marker area, request size entries with prop="sizes":
points = ax.scatter(x, y, s=sizes)
handles, labels = points.legend_elements(prop="sizes")
ax.legend(handles, labels, title="Size")
If the sizes you passed to scatter() were calculated from another quantity, the generated labels otherwise describe the size mapping rather than necessarily showing that original quantity. Pass func as the inverse of your size transformation when the legend should display the original values. For example, if your plotting values were produced by multiplying the original values by a factor, the inverse divides by that factor:
factor = 4
points = ax.scatter(x, y, s=original_values * factor)
handles, labels = points.legend_elements(
prop="sizes",
func=lambda plotted_sizes: plotted_sizes / factor,
)
ax.legend(handles, labels, title="Original value")
Choose an inverse that matches the transformation actually used to compute the plotted sizes.
Explain color and size with two legends
When both color and size carry information, generate a legend for each encoding and give each a clear title. Matplotlib replaces an Axes’ existing legend when another is created, so add the first legend back as an artist before creating the second.
points = ax.scatter(x, y, c=classes, s=sizes)
color_legend = ax.legend(
*points.legend_elements(prop="colors"),
title="Class",
loc="upper left",
)
ax.add_artist(color_legend)
size_handles, size_labels = points.legend_elements(prop="sizes", alpha=0.6)
ax.legend(size_handles, size_labels, title="Size", loc="lower right")
The distinct positions keep the two explanations separate; adjust them if either legend obscures points or important plot details. Matplotlib’s scatter legend gallery demonstrates this two-legend sequence.
Free tools Windows power users keep installed
One-click scans. No signup required.
Best Value
Position a legend and fix an empty one
Use loc to select a standard position. Use bbox_to_anchor when you need to control the anchor point or position a legend relative to the Axes or Figure. The available placement behavior is documented in the figure API.
If ax.legend() produces no entries, check that the plotted artists have labels. Automatic legend discovery excludes labels beginning with an underscore, which is also the default label behavior; without labeled artists, the legend is empty. Set a label when creating the artist or later with set_label().
When automatic discovery is not suitable, pass handles and labels explicitly:
ax.legend(handles, labels)
Keep the two sequences in the same order: the label at each position is associated with the handle at that position. Matplotlib’s pyplot legend reference discourages supplying labels alone for existing artists, since relying on implicit order can associate text with the wrong plotted item.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesFor an authoritative reference on labels, handles, placement, and legend arguments, see the pyplot legend documentation.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




