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

Multi-Line Plots

Sometimes a story has more than one character. You don't just want to see the maximum temperature, and then separately look at the minimum — you want to see them together. That is exactly what multiple line plots are for.

1The “why”: comparing stories on one page

Put a week of Gangtok's maximum and minimum temperatures on the same chart and two insights appear that neither chart alone could give you:

  • Comparison — the vertical gap between the lines is the daily temperature range. You can see instantly which day was most extreme.
  • Trends — is the week getting warmer or colder? Are the mornings getting chillier while the afternoons stay hot?

Putting related datasets on one chart makes comparisons clear and reveals insights you would miss on separate graphs.

2The “data”: side-by-side storytellers

For multiple lines, your data needs to be organised like this:

  • One common X-axis — usually the time component. Both lines share the same days_of_week timeline.
  • Multiple Y-axis datasets — each line gets its own list of values. One for max_temperatures, one for min_temperatures. Each list tells the story of one trend.
Both lines must share one scale
Two lines belong on one chart only when they measure the same thing in the same units — here, °C. Never plot, say, temperature and rainfall together by giving them two different Y-axes: the gap between the lines would then mean nothing at all, and readers will draw conclusions that are not there. Different units? Draw two charts.

3The “plotting”: one call per line

Drawing multiple lines is surprisingly simple — you just call plt.plot() once for each line. Each call gets its own Y data, and can carry its own style:

  • color='red' for the maximum, suggesting heat.
  • color='blue' for the minimum, suggesting cold.
  • label="…" — a name tag for the line, which the legend will use later.

4The “legend”: your chart's translator

With more than one line, how does anyone know which is which? That is where the legend steps in. Try switching it off below while both lines are drawn — the chart immediately becomes a guessing game.

Two stories, one chart

Draw each line, then try turning the legend off while both are showing.

14161820222426MonTueWedThuFriSatSunMax Temperature (°C)Min Temperature (°C)Weekly Temperature Trends in GangtokDay of the WeekTemperature (°C)
import matplotlib.pyplot as plt
days_of_week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
max_temperatures = [22, 24, 23, 25, 26, 24, 23]
min_temperatures = [15, 16, 15, 17, 18, 16, 15]
plt.plot(days_of_week, max_temperatures, color="red",
         label="Max Temperature (°C)")
plt.plot(days_of_week, min_temperatures, color="blue",
         label="Min Temperature (°C)")
plt.title("Weekly Temperature Trends in Gangtok")
plt.xlabel("Day of the Week")
plt.ylabel("Temperature (°C)")
plt.legend(loc="upper left")
plt.show()

plt.legend() gathers the label= from every plt.plot() call and draws the key. Now the chart explains itself.

The legend works in two steps, and both are required:

  • The magic label= — in each plt.plot() call you attach a name tag to that line.
  • The plt.legend() call— made once, after all your lines are plotted. It gathers up every label and draws a neat box showing each line's style next to its name.
Labels without a legend do nothing
A label= with no plt.legend() call is invisible — matplotlib remembers the name but never draws it. And plt.legend() with no labels produces an empty box. You need both.

5Putting it all together

Here is the complete program that recreates the weekly temperature chart. Run it, then try changing a colour, moving the legend, or adding a third line for the average temperature.

weekly_temperature.py

6Recap

CallWhat it doesHow many times?
plt.plot(x, y, color=…, label=…)Draws one lineOnce per line
plt.title() / xlabel() / ylabel()Describes the chartOnce
plt.legend(loc=…)Draws the key from the labelsOnce, after all plots
plt.savefig(…)Saves the chart to an image fileOnce, before show()
plt.show()Displays the finished chartOnce, last
Quick Check

How do you draw two lines on the same chart?

Quick Check

You set label= on both plots but the chart shows no key. What is missing?

Quick Check

What does loc='upper left' do?