LambdaLab
Informatics Practices · Class 12 · Data Visualization
Data VisualizationHands-on⏱️ 9 min read

Line Plots

Ready to transform raw data into a compelling visual story? A line plot connects a series of data points with a continuous line, which makes it perfect for showing a trend or progress over time.

We are going to work backwards. Instead of writing code and hoping for a chart, we'll take a finished chart, treat it as a blueprint, identify every piece, and then write the code that produces each piece.

1Analyzing the blueprint

Here is our target chart: a cricketer's runs across five matches. Every visible element was put there by one specific line of code. Hover any part of the chart — or any line of the program — to see them connect.

Anatomy of a chart

Hover the chart or the code — each part lights up its partner.

02040608010012012345Player's Runs Scored Per MatchMatch NumberRuns Scored
import matplotlib.pyplot as plt
 
matches = [1, 2, 3, 4, 5]
runs_scored = [45, 21, 103, 12, 88]
 
plt.plot(matches, runs_scored)
plt.title("Player's Runs Scored Per Match")
plt.xlabel("Match Number")
plt.ylabel("Runs Scored")
plt.show()
Hover a part to connect it to its code…

Reading the chart, four elements stand out:

  • Chart Title— “Player's Runs Scored Per Match”, telling us what the whole chart is about.
  • X-Label— “Match Number”, explaining the horizontal axis.
  • Y-Label— “Runs Scored”, explaining the vertical axis.
  • Data Points — the line is made by connecting individual points, each one a match number paired with the runs scored in it.

And reading the values off the chart gives us our data:

Match12345
Runs scored45211031288

So our X-axis data is [1, 2, 3, 4, 5] and our Y-axis data is [45, 21, 103, 12, 88].

2Crafting the Python code

Think of matplotlib.pyplot as your art kit for data. Each toggle below adds one real statement to the program — flip them on one at a time and watch the chart assemble itself.

Build the chart, one line at a time

Flip a switch to add that statement to your program.

02040608010012012345
import matplotlib.pyplot as plt
matches = [1, 2, 3, 4, 5]
runs_scored = [45, 21, 103, 12, 88]
plt.plot(matches, runs_scored)
plt.show()

⚠️ An unlabelled chart is a riddle. Someone seeing this has no idea what the numbers mean.

Putting it together, here is the complete program. Run it, then break it:

line_chart.py

3What each line does

  • import matplotlib.pyplot as plt— opening your art kit and saying “I'll call these tools plt for short.”
  • matches and runs_scored — your ingredients. One list for the horizontal positions, one for the vertical.
  • plt.plot(matches, runs_scored) — this is where you draw the line. The first argument places points across the X-axis, the second sets how high each one sits.
  • plt.title() is the big heading, plt.xlabel() labels the “floor”, and plt.ylabel() labels the “wall”.
  • plt.savefig("name.png") — takes a picture of your drawing and saves it as a file you can share. It must come before plt.show().
  • plt.show() — the exciting part. It makes your drawing pop up on screen, and then clears the figure.
The order matters
Every plt call decorates the current figure, and plt.show() displays it and then clears it. So all your plot(), title() and label() calls must come before show(). Call show() first and you will get an empty chart followed by a blank one.
Save before you show
Because plt.show() clears the figure, calling plt.savefig() after it will quietly save a blank image — a classic bug that is maddening to track down. Put savefig() before show(), and you get both the file and the window.
Quick Check

Which function draws the actual line on a line chart?

Quick Check

In plt.plot(matches, runs_scored), what does the first argument control?

Quick Check

You wrote plt.show() before plt.title(). What happens?