Your First Line Plot

Matplotlib is a Python library for creating charts and visualizations — and the line plot is its workhorse, perfect for showing how a value changes across an ordered sequence like time.

Learn Your First Line Plot in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll plot a single line, layer several series on one chart, and learn the shortcut for plotting y-values with an implicit x-axis.

The core call is plt.plot(x, y) . You hand it two equal-length lists — the x-values and the y-values — and Matplotlib connects the resulting points with a line.

What you'll see: a single line that climbs from 50 in 2019, dips slightly in 2021, then rises sharply to 120 by 2023 — the years run along the bottom and revenue up the side.

To compare two datasets, just call plt.plot() more than once before plt.show() . Every call adds a line to the same chart, and Matplotlib picks a fresh color for each.

What you'll see: two lines in different colors. The first (Product A) rises steadily to 65, while the second (Product B) stays lower and flattens near the end — making the gap between them easy to read.

If you pass a single list, Matplotlib uses its index positions (0, 1, 2, ...) as the x-values. You can also pass several x, y pairs in one plt.plot call.

What you'll see: first a single zig-zag line indexed 0 through 4. Then a second window with two lines — a steep curve (the squares) and a straight diagonal — both drawn from one plt.plot call.

Replace each ___ to draw two lines on one chart.

❌ ValueError: x and y must have same first dimension

Your x and y lists are different lengths. Count the items in each and make them equal.

Put both plt.plot() calls before a single plt.show() . A show() in between clears the figure.

Plot a week of temperatures for two cities on the same chart.

Lesson 3 complete — you can draw lines!

You plotted single and multiple lines, used an implicit x-axis, and drew several series in one call.

🚀 Up next: Labels, Titles & Legends — make your charts readable and self-explanatory.

Practice quiz

What does plt.plot(x, y) do with two equal-length lists?

  • Draws a bar per value
  • Makes a scatter only
  • Creates a pie chart
  • Connects the (x, y) points with a line

Answer: Connects the (x, y) points with a line. plt.plot(x, y) connects the points into a line.

How do you draw two lines on the same chart?

  • Call plt.subplots(2)
  • Call plt.plot() twice before plt.show()
  • Pass two=True
  • Use plt.lines()

Answer: Call plt.plot() twice before plt.show(). Each plt.plot() call before show() adds another line to the same Axes.

What does plt.plot(y) do when given a single list?

  • Uses index positions 0,1,2,... as the x-values
  • Raises an error
  • Plots a horizontal line
  • Uses random x-values

Answer: Uses index positions 0,1,2,... as the x-values. With one list, Matplotlib uses the list's index positions as x.

What does plt.plot(x, y1, x, y2) draw?

  • A single averaged line
  • Nothing
  • Two lines in one call
  • A scatter plot

Answer: Two lines in one call. Repeated x, y pairs in one call draw multiple lines.

What does Matplotlib do with the color of each new line on one Axes?

  • Keeps them all black
  • Uses random colors each run
  • Errors on the second line
  • Automatically gives each a different color

Answer: Automatically gives each a different color. Matplotlib cycles through its color cycle so each line differs.

What raises 'ValueError: x and y must have same first dimension'?

  • Calling show() too early
  • x and y lists of different lengths
  • Plotting only one line
  • Using integer data

Answer: x and y lists of different lengths. x and y must contain the same number of items.

Why might only one line appear when you expected two?

  • The colors matched
  • The data was too large
  • A plt.show() between the calls cleared the figure
  • You used plt.plot once

Answer: A plt.show() between the calls cleared the figure. A show() in between clears the figure, so put both plots before a single show().

Which call finally displays all the queued lines?

  • plt.show()
  • plt.render()
  • plt.flush()
  • plt.paint()

Answer: plt.show(). plt.show() displays everything drawn so far.

When two series share the same x-values, what should you do?

  • Use two figures
  • Sort one list
  • Halve the y-values
  • Reuse the same x list for both plot calls

Answer: Reuse the same x list for both plot calls. Reusing the same x list keeps the lines aligned on the same axis.

A line plot is best suited for which kind of data?

  • Unordered categories
  • An ordered sequence such as values over time
  • Geographic coordinates only
  • Single numbers

Answer: An ordered sequence such as values over time. Line plots show how a value changes across an ordered sequence like time.