Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →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.
#1 Best Overall
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.
Rank #2
- Use a
NaNor 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.
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.
Quick Recap
Best Value
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
NaNvalue 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.




