Matplotlib · Chapter 4 of 16
Lines
By the end of this chapter you can draw several lines on one Axes and tell them apart by colour, width, dashes and markers, put real dates on the x axis, keep the rows in order so the line does not zigzag, show a gap in the data as a gap, shade a band between two lines, and draw a line straight across the chart at a standard such as 60 µg/m³. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The first chart
- 4 tasks
- Matplotlib
- In the all labs plan
The first 2 chapters of Matplotlib are free. This one comes with the all labs plan.
Why you need this
A line chart is SaafHawa's most common chart, and the four people who use it each found a way it goes wrong:
- Lalitha prints the monthly report in black and white as often as in colour. Two cities that differ only in colour become two grey lines nobody can tell apart.
- Jaspreet explains every January why the chart for 14 January has a straight line through the night: SaafHawa's server was down for 30 hours, and a line drawn across those hours shows readings that were never taken.
- Ira wants Diwali week on one chart. Six stations are six tangled lines; what she means is "the range of the six, hour by hour, against the standard".
- And one of Ira's charts of a month came out as a scribble, the line jumping from one end of the month to the other and back.
Everything you know so far draws a line through points in the order you give them, in colours Matplotlib picks. This chapter takes over each of those choices.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
two_styles.py: Delhi and Gurugram in two styles
Write two_styles(dates, delhi, gurugram) in two_styles.py. dates are the days of a month as dates (city_daily.csv's date column after pd.to_datetime), and delhi and gurugram are each city's daily PM2.5 on those days. Make a new Figure and Axes and draw delhi against dates and gurugram against dates as two lines, with these styles exactly:
Delhi: the colour black, 2 points wide, a solid line, no markers, labelled Delhi.
Gurugram: the colour "tab:green", 1 point wide, dashed ("--"), with the marker "o" (a round dot) at every point, labelled Gurugram.
Add a legend. It returns the Figure, with one Axes, the two lines with dates along x, and a legend of Delhi and Gurugram. The Check tries November 2025 and 1 to 10 December 2025, and reads each line's colour, width, line style and marker.
- Task 2
gap_chart.py: the server gap, kept as a gap
Write gap_chart(hours) in gap_chart.py. hours is a DataFrame of consecutive rows from delhi_hourly.csv or diwali_hourly.csv, with a column time (text such as "2026-01-14 03:00", one row per hour) and a column pm25, which is NaN in the hours with no reading. Make a new Figure and Axes and draw pm25 against the times, turned into dates, as one line, with every row kept: the hours with NaN stay NaN, so the line is broken there.
It returns the Figure, with one Axes and one line whose x values are the times as dates (a date axis, not text) and whose y values are pm25, NaN included. The Check tries Delhi from 13 to 16 January 2026 (the 30-hour server gap) and Saket from 22 to 24 October 2025 (6 hours missing), and fails a line that skips or fills in the empty hours.
- Task 3
diwali_band.py: the Diwali band
Write diwali_band(times, low, high, mean) in diwali_band.py. times are hours as dates, and low, high and mean are the six Diwali stations' lowest, highest and mean PM2.5 at each of those hours, from diwali_hourly.csv. Make a new Figure and Axes and draw on it:
one shaded band between low and high against times (filled from low up to high, not from 0);
mean against times as one line (the only line drawn through data points);
a horizontal line across the whole chart at 60, India's 24-hour standard.
It returns the Figure, with one Axes. The Check tries 18 to 23 October 2025 and 20 October alone. It checks that the band runs along low and high at every hour, that the one line is the mean, and that the line at 60 spans the chart rather than being plotted through two points.
- Task 4 · interview question
Interview: zigzag.py, the line that zigzagged
Ira's month_line(days) draws a month of a city's days, and its chart is a scribble: the line jumps back and forth across the month. days is a DataFrame from city_daily.csv with a column date (text such as "2025-11-05") and a column pm25, one row per day, in no particular order. Fix it so the chart is one line through the month in date order: the dates along x as dates, from the first date to the last, and each day's pm25 up y.
It returns the Figure, with one Axes and one line. The Check tries Noida's November 2025 in a shuffled order and Faridabad from 1 to 14 March 2026, newest first, and fails a line whose x goes back in time, or whose readings are no longer with their own days.
Tanvir will ask: in what order does plot join the points, why did this make a zigzag, and why did the dates on the axis still look right?
What you will learn
- Several lines and the colour cycle: each line takes the next colour, "C0", "C1" and on.
- Dates on x: pd.to_datetime, and how Matplotlib turns dates into numbers.
- x in order: plot joins the rows in the order they come.
- Style: color=, linewidth=, linestyle=, marker=, markersize=, alpha= and format strings like "o-".
- Gaps: a NaN breaks the line; dropping it joins across.
- ax.fill_between, ax.axhline, ax.axvline: a band, and lines across the chart.
- Relatives: ax.step, ax.errorbar, ax.stackplot.
More in The first chart
Build "Lines" yourself
SaafHawa runs air sensors across Delhi NCR. Principals, societies and investors need its numbers at a glance: you draw them. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Matplotlib are free. This one comes with the all labs plan.