Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

Change a Matplotlib Scatter Legend’s Background Color in Python

Use facecolor in ax.legend() to set a scatter legend background, or update an existing legend with get_frame().set_facecolor().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set facecolor in ax.legend() to choose a scatter plot legend’s background color:

leg = ax.legend(facecolor="lightyellow", edgecolor="0.3", framealpha=1)

This changes the legend frame, not the scatter markers. If the legend already exists, set the frame color with leg.get_frame().set_facecolor("lightyellow").

As an Amazon Associate I earn from qualifying purchases.

Set the legend background when creating it

Pass facecolor to Axes.legend when you create the legend. This example labels two scatter series and gives the legend an opaque white background with a gray border:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(x_a, y_a, c="tab:blue", label="Group A")
ax.scatter(x_b, y_b, c="tab:orange", label="Group B")
ax.legend(facecolor="white", edgecolor="0.25", framealpha=1)

plt.show()

Replace x_a, y_a, x_b, and y_b with your data arrays. The Matplotlib Axes.legend API describes facecolor as the legend’s background color.

Change the facecolor of a legend that already exists

Keep the legend object returned by ax.legend(), then update its frame:

leg = ax.legend()
leg.get_frame().set_facecolor("lightyellow")

This is useful when a legend was created elsewhere or its appearance is being adjusted later. Matplotlib’s legend gallery also demonstrates changing the frame with legend.get_frame().set_facecolor('C0').

Why the legend can match the axes background

By default, the legend facecolor comes from rcParams["legend.facecolor"], whose default is "inherit". Inheritance uses axes.facecolor, so the legend can appear to share the axes background. Set an explicit facecolor to override that behavior. See the Matplotlib configuration guide for the relevant defaults.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Control the frame and markers independently

The legend frame has separate settings for its background, border, and transparency:

  • facecolor sets the legend background.
  • edgecolor sets the frame border color.
  • framealpha sets the background transparency.

These options do not recolor the scatter data or its legend markers. Set marker colors on the scatter calls themselves, or use the handles generated for mapped colors or sizes. Matplotlib’s collections API documents scatter legend elements; the scatter plot guide covers scatter plots. For legends with scatter markers, scatterpoints controls how many points appear in each entry.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the right method

Situation Use
Creating the legend ax.legend(facecolor="...")
Legend already exists leg.get_frame().set_facecolor("...")

The cited API reference identifies itself as Matplotlib 3.11.2. If you need behavior specific to another release, check the documentation for the version installed in your environment.

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.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.