📈
Coding🎓 Ages 14-18Intermediate 10 min read

Line Charts in Python with Matplotlib

Plot line charts in Python with Matplotlib: draw a single line, add titles and labels, plot multiple lines with a legend, and chart real data with a worked example.

Key takeaways

  • Matplotlib's plt.plot draws a line through your x and y data points
  • Always label your axes and add a title so the chart is readable
  • Call plt.plot more than once to draw several lines, then add a legend
  • plt.show() displays the chart; plt.savefig() saves it as an image file

What a line chart shows

A line chart connects data points with a line to reveal how a value changes across a continuous range — temperature through the day, sales over months, or a score over several weeks. The slope of the line tells you at a glance whether things are rising or falling.

Python's most popular charting library is Matplotlib. We will use its pyplot part, almost always imported as plt. If you have already met bar charts in Python with Matplotlib, much of the setup will feel familiar.

Installing and importing

Matplotlib is an external library, so install it once from your terminal:

pip install matplotlib

Then import it at the top of your program:

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt brings in the plotting tools and gives them the short nickname plt, which everyone uses. Installing libraries is covered in using Python libraries.

Your first line chart

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
temps = [12, 15, 14, 18, 20]

plt.plot(days, temps)
plt.show()

Line by line:

  • days holds the x-values (the horizontal positions) and temps holds the y-values (the heights).
  • plt.plot(days, temps) draws a line connecting the points (1,12), (2,15), (3,14) and so on. The two lists must be the same length.
  • plt.show() opens a window displaying the chart.

That is a working chart, but it lacks labels. A chart without context is hard to trust.

Adding titles and labels

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
temps = [12, 15, 14, 18, 20]

plt.plot(days, temps)
plt.title("Temperature This Week")
plt.xlabel("Day")
plt.ylabel("Temperature (C)")
plt.show()
  • plt.title(...) adds a heading above the chart.
  • plt.xlabel(...) and plt.ylabel(...) describe the axes so a reader knows what the numbers mean.

Always label your charts. An unlabelled axis is one of the most common reasons a graph is misunderstood.

Styling the line

You can change the colour, line style and add markers at each point:

import matplotlib.pyplot as plt

days = [1, 2, 3, 4, 5]
temps = [12, 15, 14, 18, 20]

plt.plot(days, temps, color="green", marker="o", linestyle="--")
plt.title("Temperature This Week")
plt.xlabel("Day")
plt.ylabel("Temperature (C)")
plt.show()
  • color="green" sets the line colour.
  • marker="o" puts a small circle at every data point.
  • linestyle="--" makes the line dashed.

These options are optional, but markers help readers see exactly where the data points sit.

Plotting multiple lines

Comparisons are where line charts shine. Call plt.plot once per line, give each a label, then call plt.legend().

import matplotlib.pyplot as plt

months = [1, 2, 3, 4, 5, 6]
london = [5, 7, 10, 13, 16, 19]
cairo = [14, 16, 20, 25, 29, 32]

plt.plot(months, london, marker="o", label="London")
plt.plot(months, cairo, marker="o", label="Cairo")

plt.title("Average Temperature by Month")
plt.xlabel("Month")
plt.ylabel("Temperature (C)")
plt.legend()
plt.show()
  • Each plt.plot call adds another line to the same figure.
  • The label= text is what appears in the key.
  • plt.legend() draws that key so readers can tell the lines apart.

Both lines share one set of axes, making the contrast between the two cities obvious.

Worked example: charting saved data

Often your numbers live in a list of records rather than two neat lists. Here we reshape data before plotting.

import matplotlib.pyplot as plt

# Each record is (week, score)
records = [(1, 60), (2, 68), (3, 65), (4, 72), (5, 80)]

weeks = [r[0] for r in records]
scores = [r[1] for r in records]

plt.plot(weeks, scores, marker="o", color="purple")
plt.title("Test Scores Over Time")
plt.xlabel("Week")
plt.ylabel("Score")
plt.savefig("scores.png")
print("Chart saved as scores.png")

Walk through it:

  • weeks = [r[0] for r in records] is a list comprehension that pulls the first item from each tuple. scores pulls the second. These give the two equal-length lists plt.plot needs. Comprehensions are explained in Python list comprehensions.
  • plt.savefig("scores.png") saves the chart as an image instead of opening a window — useful when your code runs on a server with no screen.

The result is a clear upward line showing improving scores week by week.

Common mistakes

  • Mismatched list lengths. plt.plot(x, y) fails if x and y have different numbers of items.
  • Forgetting plt.show() or plt.savefig(). Without one of them, the chart is built but never displayed or saved.
  • Calling plt.show() between lines. That opens the first chart and clears it before the second line is added. Add all lines first, then show once.
  • No legend for multiple lines. Without plt.legend(), readers cannot tell which line is which.

Try it yourself

  1. Plot your own daily step count for a week and label both axes.
  2. Plot two lines comparing the high and low temperature each day, with a legend.
  3. Build a list of (hour, visitors) tuples, split it into two lists with comprehensions, and chart it, saving the result as a PNG.

For comparing fixed categories instead of trends, revisit bar charts in Python with Matplotlib.

Quick quiz

Test yourself and earn XP

Which function draws a line chart in Matplotlib?

What does plt.show() do?

How do you label the horizontal axis?

To draw two lines on the same chart you should...

What makes a legend appear?

FAQ

Yes. Matplotlib is not built into Python. Install it once from the command line with: pip install matplotlib. After that you can import it in any program.

A line chart connects points to show change over a continuous range, like a value over time. A bar chart compares separate categories. Use a line chart when the x-axis represents something that flows, such as months or hours.

Replace plt.show() with plt.savefig('chart.png'). This writes an image file you can share or embed, which is handy when running code without a screen.