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 desk3 min

How to Plot Multiple Lines of Different Lengths in Matplotlib

Plot each independent series with its own Matplotlib call. Learn when grouped inputs, 2D arrays, NaN gaps, and explicit line styles make sense.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call ax.plot(x, y) once for each line. Each call accepts its own x and y arrays, so independent series can have different numbers of points; within each series, x and y must still contain matching coordinates.

Plot each unequal-length series in a separate call

This is the clearest approach when lines have independent observations or sampling grids. The Matplotlib plot API describes repeated calls as the most straightforward way to draw multiple datasets, and its quick-start guide uses successive Axes.plot calls.

import matplotlib.pyplot as plt

x1 = [0, 1, 2, 3]
y1 = [1, 3, 2, 4]
x2 = [0, 1, 2, 3, 4, 5]
y2 = [2, 1, 3, 2, 4, 3]

fig, ax = plt.subplots()
ax.plot(x1, y1, marker="o", label="Series A")
ax.plot(x2, y2, marker="s", label="Series B")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()
plt.show()

The first call draws four points and the second draws six. Matplotlib does not require the separate lines to have equal lengths. It does require each call’s x and y values to correspond point by point: the x value at a given position is paired with the y value at that position.

Choose the input form that matches your data

Input form When it fits Shape and styling considerations
Separate ax.plot(x, y) calls Independent series, especially when they have unequal lengths or different x coordinates. Each call pairs one x series with one y series; styling and labels can be set independently.
Grouped arguments in one call Several datasets that you want to pass together, such as ax.plot(x1, y1, "-", x2, y2, "--"). Each x/y group must still describe matching points. Keyword style properties apply to all lines unless formatting is specified for each group.
Two-dimensional x and y arrays Datasets organized in compatible rectangular arrays, typically with a shared shape or dimension. If both are 2D, x and y must have the same shape. If one is 2D with shape (N, m), the other must have length N and is reused for the m datasets. This is generally unsuitable for unrelated, unequal-length series.

These constraints are documented in the Matplotlib plot API. Do not force irregular series into a rectangular array merely to make one call: separate calls preserve their original lengths without padding.

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.

Use implicit x coordinates only when the sample index is the x value

Passing just a y series, as in ax.plot(y), makes Matplotlib use indices from zero through len(y) - 1 for its x coordinates. Separate calls then index each y series independently. This works when the horizontal axis means “sample number”; use explicit x arrays when the observations have meaningful coordinates such as dates, times, or measurements.

Represent missing observations according to the intended line

Unequal lengths alone are not a reason to pad. Plot each independent x/y pair as-is. Padding becomes relevant only if the data represent a shared grid with intentionally missing observations.

  • Use a NaN or masked value at a missing position when the plotted line should visibly break there. Matplotlib’s masked and NaN values example shows that these values break the line and suppress a marker at that point.
  • Remove a missing point only when connecting the remaining neighboring observations is an honest representation. Removing points draws a continuous line between the points that remain, which can imply continuity across the missing interval.

Make each line easy to identify

Give every series a label and call ax.legend() so readers can tell which line represents which dataset. Matplotlib cycles through its default line styles; set properties explicitly when colors, markers, or line styles need to be stable or more distinguishable.

ax.plot(x1, y1, color="tab:blue", marker="o", label="Series A")
ax.plot(x2, y2, color="tab:orange", marker="s", linestyle="--", label="Series B")
ax.legend()

The plot API also accepts a format string, such as "bo", as a shortcut for line properties. Named options such as color, marker, and linestyle make the choices more explicit. The quick-start guide covers the basic plotting workflow.

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

Handle many line segments with a collection when appropriate

For a large collection of line segments, Matplotlib provides LineCollection; its input representation and styling workflow differ from ordinary plot calls. It is a batch-rendering option, not a workaround for mismatched x and y coordinates. See Matplotlib’s LineCollection example.

Check these issues if the plot fails or looks wrong

  • A line has mismatched coordinates: verify that x and y in that particular call refer to the same observations and have compatible lengths.
  • You tried to combine irregular series in a 2D array: use one call per series unless your data genuinely fit the documented 2D shape rules.
  • The line connects across a missing observation: use a masked or NaN value if the chart should show a break; deleting the point joins its neighbors.
  • Lines are hard to distinguish: add labels and a legend, then use markers or explicit line styles rather than relying only on color.

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 *

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.