Plot each data series against a shared x-axis with ax.plot(x, y, label="Series name"), then call ax.legend() to identify the lines. For time series, pass dates as Python datetime values or NumPy datetime64 values; Matplotlib handles date conversion and date-aware ticks.
Plot multiple lines on one chart
When the series share the same x-values, call plot once for each y-series. Separate calls make each line’s label and style straightforward to control:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(layout="constrained")
ax.plot(x, series_a, label="Series A")
ax.plot(x, series_b, label="Series B")
ax.set_xlabel("Time")
ax.set_ylabel("Value")
ax.legend()
plt.show()
Use numeric x-values for an ordinary line plot, or date/time values for a time series. The Matplotlib plot API returns line objects and supports labels and styling options such as color, linestyle, and markers.
Label and distinguish each series
Give every line a useful label and call ax.legend(); otherwise, readers may not know which line represents which series. If colors alone are not enough to distinguish the lines, vary their linestyles or markers as well.
#1 Best Overall
Use one call for several x/y pairs
Matplotlib also accepts multiple x/y pairs in a single plot call. This is compact when the lines share formatting: keyword styling applies to all lines in that call. Use separate calls when lines need individual labels or styles.
Plot dates on a time-series axis
Pass date-aware values directly instead of turning timestamps into arbitrary strings. Matplotlib supports Python datetime and NumPy datetime64 values, converts them for plotting, and applies automatic date tick location and formatting. See the date units documentation.
Rank #2
Matplotlib connects points in the order supplied. If the records are not already chronological, sort them by timestamp first; otherwise, the line can move backward and forward across the date axis.
Control tick spacing and labels
Automatic date ticks are often sufficient. For a dense chart or a long date range, use tools from matplotlib.dates to choose a cadence or format, including AutoDateLocator, AutoDateFormatter, ConciseDateFormatter, MonthLocator, and DateFormatter. The dates API documents these options.
Choose how missing dates affect spacing
Actual datetimes and observation indices produce different horizontal spacing. Choose according to what the x-axis should communicate:
| Approach | Horizontal spacing | Useful when |
|---|---|---|
| Plot actual datetime values | Proportional to elapsed calendar time; gaps remain visible. | The length of a gap matters to interpretation. |
| Plot successive observation indices and format those positions as dates | Equal between records; missing dates take up no space. | You want, for example, daily observations to have equal spacing despite weekends or other non-observation days. |
The Matplotlib time-series date index formatter example demonstrates the index approach. It changes the meaning of horizontal distance: the positions represent record order, not elapsed calendar time.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Account for date precision when plotting very fine timestamps
Matplotlib represents dates as floating-point days from its default epoch, 1970-01-01 UTC. The dates API describes microsecond precision as achievable within approximately 70 years of that epoch, with lower precision farther away. Routine daily or monthly charts are not the concern; for sub-microsecond time plots, the API recommends plotting floating-point seconds instead.
The stable documentation pages cited here identify Matplotlib 3.11.2 for the plot and date API references, and 3.11.0 for the index-formatter example. If you maintain code on an older Matplotlib release, check the documentation for that installed version.
Recommended Free Tools
Quick Recap
Best Value
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.




