HOWTO · Matplotlib
How to Connect Scatterplot Points With Line in Matplotlib
Connect Matplotlib scatterplot points in data order with plot(), style the line, and control which layer appears on top with zorder.
On this page
To connect scatterplot points in their data order, pass the same x and y values to scatter() and plot(), then call show() to display the figure. scatter() draws the markers as an independent collection, plot() draws the connecting path through the points in the order supplied, and show() displays the completed figure.
The examples in this tutorial were run and verified with Matplotlib 3.11.2 on Python 3.12, and every figure below is the rendered output of the example directly above it.
Connect scatterplot points in data order with scatter() and plot()
Give both functions the same coordinate sequences. plot() connects each pair in the order supplied, so the sequence determines the path; Matplotlib does not sort points by x-coordinate. Keep each x value paired with its corresponding y value: the first x value belongs with the first y value, the second with the second, and so on. If you sort observations by x, reorder the y values together with those same observations rather than sorting the two lists independently.
This matters when the data represents a route, a time series, or another sequence where order carries meaning. For example, x values that move from 1 to 3 and then back to 2 produce a path that moves right and then left. That can be correct for a trajectory. If your goal is instead a curve progressing from left to right, sort the paired observations first. A line can also contain vertical segments when neighboring observations share an x value; plot() still joins them in the supplied order.
This example overlays the markers and the connecting line using one set of coordinates:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 1, 3, 2]
plt.scatter(x, y)
plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Using the same x and y inputs keeps the markers and line aligned. If the two layers appear offset, check that neither sequence was transformed or reordered for only one call. For data stored in a table, select both columns from the same rows and in the same order. The example uses the pyplot interface, so it does not need a separate figure variable; with the object-oriented interface, create a figure and axes with fig, ax = plt.subplots(), then call ax.scatter(...) and ax.plot(...) on that axes.
Style the line and markers with plot()
Use linestyle and color to style the path drawn by plot(). The next example keeps a separate scatter() layer for the markers and draws the connecting path as a dashed blue line:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 1, 3, 2]
plt.scatter(x, y, color="blue")
plt.plot(x, y, linestyle="--", color="blue")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Calling scatter() and plot() separately gives independent control over the point collection and the connecting path: marker size and color can change without touching the line, which is useful when points need stronger emphasis while the path stays visually simple. Give both calls the same coordinate sequences so each marker sits on its corresponding point in the line.
A single plot() call can also draw both layers at once through a format string such as "xb-", where x selects the marker shape, b selects blue, and - selects a solid line. The format string is compact when one call should draw everything; keyword arguments such as color, marker, and linestyle are clearer when you need other properties, such as marker size, line width, or transparency. Whichever approach you choose, show() belongs after all styling and layer calls, because it displays the figure only after those artists have been added.
Control which layer appears on top with zorder
The artist with the higher zorder is drawn above the one with the lower value. This only changes drawing order; it does not change the coordinates or the path. The setting matters when a thick line crosses a marker and hides its center, or when the line should stay visible across dense markers.
The next example makes the line deliberately thick and gives the markers the higher zorder, so each round marker stays whole where the line passes underneath it:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 1, 3, 2]
plt.plot(x, y, linewidth=6, zorder=2)
plt.scatter(x, y, s=120, zorder=3)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
For markers above the line, give the scatter collection the larger value, as in zorder=3 for scatter() against zorder=2 for plot() above. To put the line above the markers, swap the values. Use a modest difference between the two values to express the intended order, and set zorder on both artists when the layering should be unambiguous. The same comparison applies to any other plot elements you add later: the artist with the higher zorder is drawn over the others.
Match the lengths of x and y to avoid the size mismatch error
Both functions pair the values position by position, so the two sequences must have the same length. When they do not, the call raises a ValueError and no figure is drawn. This example drops the last y value by mistake:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 1, 3]
plt.scatter(x, y)
plt.plot(x, y)
plt.show()
Running it fails at the scatter() call with:
ValueError: x and y must be the same size
Calling plot() alone with the same inputs fails as well, with its own wording of the same problem: ValueError: x and y must have same first dimension, but have shapes (4,) and (3,). To fix the error, find the missing or extra observation and make the two sequences describe the same points again. Comparing len(x) and len(y) before plotting pinpoints which side is short. When the data comes from a table, filter the rows once and then take both columns from the filtered rows, instead of filtering each column separately and letting their lengths drift apart.
Two empty sequences are a different case: they are accepted and produce an empty set of axes without an error. An empty figure therefore usually means the input data was empty, not that the lengths mismatched.