plot() (Matplotlib)

ax.plot() draws a line plot, connecting (x, y) data points with line segments. It is the most fundamental Matplotlib chart, used for trends, time series, and any data where order along x matters.

Matplotlib

The basic call ax.plot(x, y) connects the points in order. A compact format string sets style in one go: 'ro--' means red, circle markers, dashed line. You can also pass keyword arguments like color, linestyle, linewidth, marker, and label for fuller control.

Call plot several times on the same Axes to overlay multiple lines, and pass label=... to each so a later ax.legend() can identify them. If you give plot only one array it treats it as y and uses the index 0, 1, 2, … as x.

plot returns a list of the Line2D objects it created, which you can capture to tweak a line after the fact. For unconnected points use scatter; for categories use bar.

Syntax

ax.plot(x, y, fmt, **kwargs)

Examples

Python

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
line, = ax.plot([1, 2, 3], [4, 5, 6], "ro--")
print(type(line).__name__)

Output:

Line2D

Learn it in a lesson

See also

Back to the Dictionary