October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Plot Multiple Lines and Time Series with Matplotlib

Plot and label multiple lines in Matplotlib, format date axes, and choose whether missing dates create calendar gaps or equal spacing between observations.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

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.