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.
Hover the chart or the code — each part lights up its partner.
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:
| Match | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Runs scored | 45 | 21 | 103 | 12 | 88 |
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.
Flip a switch to add that statement to your program.
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:
3What each line does
import matplotlib.pyplot as plt— opening your art kit and saying “I'll call these toolspltfor short.”matchesandruns_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”, andplt.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 beforeplt.show().plt.show()— the exciting part. It makes your drawing pop up on screen, and then clears the figure.
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.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.Which function draws the actual line on a line chart?
In plt.plot(matches, runs_scored), what does the first argument control?
You wrote plt.show() before plt.title(). What happens?