Matplotlib · Chapter 9 of 16
Scales and limits
By the end of this chapter you can say how Matplotlib chooses an axis' limits, set them yourself with ax.set_xlim and ax.set_ylim (one side or both), decide when an axis must start at zero, put an axis on a log or symlog scale, draw two quantities on twin y axes with ax.twinx(), and turn an axis upside down. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Reading the numbers
- 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
Three charts went out this month, and each was right and misleading.
Ira's weekly chart for each school picks its own y axis. Gurugram's clean July, 9 to 36 µg/m³, fills the chart from top to bottom exactly like Delhi's November at 163 to 325: a principal sees the same wiggle and asks why the air is always bad. Lalitha's investor chart of monitors on subscription grows from 6 to 2,830 on a plain axis, so the first year is a flat line along the bottom, and an investor asked whether nothing happened in 2024. Jaspreet's year for Hauz Khas is a single spike, the day a spider built a web in the sensor's inlet (1,485.3 µg/m³), with the other 360 days squashed along the bottom.
The data is fine in all three. What is wrong is the range of each axis and how it is spaced, and so far Matplotlib chose both. This chapter chooses them on purpose.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
log_growth.py: growth on a log scale
Write sensor_growth(months, sensors) in log_growth.py. months is a pandas Series of dates (the month column of business_monthly.csv after pd.to_datetime) and sensors the sensors_live count for each month (the indoor monitors on subscription, 6 to 2,830). It makes a new Figure and Axes, draws sensors against months as one line, puts the y axis on a log scale, labels the y axis exactly Monitors live (log scale), and returns the Figure (one Axes, one line).
The x axis stays a plain (linear) date axis. The Check calls it with all 30 months and with the first 18 (April 2024 to September 2025), and checks the line, both scales and the label.
- Task 2
week_limits.py: the week from zero
Write week_chart(dates, pm25) in week_limits.py. dates is a pandas Series of one or two weeks of dates, in order (the date column of city_daily.csv for one city after pd.to_datetime), and pm25 the city's PM2.5 on each day. It makes a new Figure and Axes, draws pm25 against dates as one line, and returns the Figure (one Axes, one line).
The y axis starts at exactly 0, and its top is left to Matplotlib, so the week's highest day is always on the chart. The x axis runs from exactly the first date to exactly the last, with no space past either end.
The Check calls it with Delhi from 27 October to 2 November 2025 (its worst day reads 344.0), Gurugram from 6 to 12 July 2026 and Noida from 1 to 14 February 2026, and reads both axes' limits after the figure is drawn.
- Task 3
twin_axes.py: PM2.5 and wind on twin axes
Write pm_and_wind(dates, pm25, wind) in twin_axes.py. dates is a pandas Series of a month's dates (the date column of city_daily.csv for one city after pd.to_datetime), pm25 the city's PM2.5 and wind its wind speed in km/h on each day. It makes a new Figure and Axes, and returns the Figure, which ends up with two Axes on top of each other:
the first Axes, with its y axis on the left: pm25 against dates as one line in the colour "C0", its y axis labelled PM_LABEL (PM2.5 (µg/m³)), the label in "C0";
a twin of the first Axes that shares its x axis, with its y axis on the right: wind against dates as one line in the colour "C1", its y axis labelled WIND_LABEL (Wind (km/h)), the label in "C1".
The starter keeps both labels in PM_LABEL and WIND_LABEL. The Check calls it with Delhi in November 2025 and Gurugram in July 2026, and reads each Axes' line, label, line colour and label colour.
- Task 4 · interview question
Interview: zero_months.py, the months with no churn
Ira asked for the customers SaafHawa won and lost each month on a log scale, because won runs from 1 to 38 and lost from 0 to 16. Lalitha's customer_flow(months, won, lost) draws both as lines titled Customers won and lost each month, and returns the Figure. months is a pandas Series of dates (business_monthly.csv's month after pd.to_datetime), won and lost lists of counts (its new_customers and churned_customers). The won line is fine. The lost line is in pieces: the first eight months, all 0, are not on the chart, and between later months it dives off the bottom. Matplotlib says nothing.
Fix it so every month shows, the zeros too. It still returns a Figure with one Axes, two lines (won and lost against months) and the title Customers won and lost each month (exactly). Its y axis is on the scale that is linear around 0 and logarithmic beyond, so small counts still spread out like on a log scale but 0 has a place (Matplotlib calls it "symlog"), and its limits show every month, 0 included (leave them to Matplotlib).
The Check calls it with all 30 months and with April 2024 to March 2025, and reads the lines, the title, the scale and the y limits.
Tanvir will ask: on a linear axis the first year looks flat, so when is a log scale honest, and what does it do with a month of 0?
What you will learn
- Autoscale and ax.margins: how Matplotlib picks limits, 5 % past the data.
- ax.set_xlim, ax.set_ylim: fix one side or both, and ax.get_ylim() to read them.
- The zero question: when an axis must start at 0, and when it may not.
- ax.set_yscale("log") and "symlog": equal steps as equal ratios, and what happens to 0.
- ax.twinx(): a second y axis on the right, and why twins can mislead.
- ax.invert_yaxis(): an axis upside down, in one line.
More in Reading the numbers
Build "Scales and limits" 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.