October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Make Matplotlib Line Colors Clear with Palettes and Legends

Use repeated ax.plot() calls to draw multiple series together. Let Matplotlib cycle colors, assign each line explicitly, or configure a custom palette.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call ax.plot() once for each data series on the same Axes. Matplotlib assigns successive colors automatically; use color= when you need a specific palette, and add labels plus a legend to identify the lines.

Plot multiple lines with automatic colors

When you omit a color, Matplotlib takes each line’s color from the Axes property cycle. This is the simplest option when the exact colors do not matter.

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x, y1, label="Series 1")
ax.plot(x, y2, label="Series 2")
ax.legend()
plt.show()

Replace x, y1, and y2 with your data. Both calls draw on the same Axes, so the lines appear together. The labels are shown in the legend. Matplotlib cycles through the configured styles; if you plot more lines than there are entries in the cycle, styles repeat. See the pyplot.plot API reference.

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

Choose exact colors for each line

Pass a color to each call when a series needs a specific color or the same series should retain its color across plots.

ax.plot(x, y1, color="tab:blue", label="Series 1")
ax.plot(x, y2, color="#ff7f0e", label="Series 2")

Matplotlib accepts short color codes such as 'r', names such as 'green', hexadecimal strings such as '#008000', the 'tab:' Tableau color family, and cycle-index strings such as 'C0' and 'C1'. A Cn value refers to an entry in the default property cycle. The colors guide describes supported color specifications.

Set a palette for one chart or future charts

Use a custom cycle on one Axes

Set the property cycle before plotting to make its colors apply successively to lines on that Axes.

from cycler import cycler

fig, ax = plt.subplots()
ax.set_prop_cycle(color=["#4c78a8", "#f58518", "#54a24b"])
ax.plot(x, y1, label="Series 1")
ax.plot(x, y2, label="Series 2")
ax.legend()

This keeps the palette local to that chart. The Axes styling guide explains property cycles.

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

Set the default cycle for subsequently created Axes

To configure a palette for Axes created afterward, set axes.prop_cycle through Matplotlib’s runtime configuration:

import matplotlib as mpl
from cycler import cycler

mpl.rcParams["axes.prop_cycle"] = cycler(color=["#4c78a8", "#f58518", "#54a24b"])

This affects subsequently created Axes, not Axes that already exist. For the available configuration setting, see the customization guide and the configuration API reference. Changing lines.color is not the way to set the usual color cycle for plot(); use axes.prop_cycle.

Plot several datasets in one call

Repeated calls are often clearest when each series needs its own label or color. The plot() API also accepts multiple [x], y, [fmt] groups in a single call, or 2D arrays where each column represents a dataset. With 2D x and y, their shapes must match. If only one argument is 2D, the other one-dimensional sequence is reused for each dataset. Additional keyword arguments in a grouped call apply to all datasets, so repeated calls offer more direct per-series control. Consult the API reference for the accepted forms.

Make overlapping lines easier to distinguish

A legend identifies named series, while markers and line styles provide visual cues in addition to color. You can set them on each line, for example with marker="o" or linestyle="--". Color alone does not guarantee that a chart will be accessible or readable in every viewing condition; use multiple cues when lines overlap or the figure has many series.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the Matplotlib version

The stable documentation identifies its current API page as Matplotlib 3.11.2, but the stable URL can change as releases advance. If your code is pinned to a particular Matplotlib version, check that version’s documentation for behavior and supported options.

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.

Leave a Reply

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

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.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.