Matplotlib · Chapter 8 of 16
Ticks and formatters
By the end of this chapter you can say where an axis' ticks come from (a locator and a formatter), place them yourself with ax.set_xticks, turn and size them with ax.tick_params, set a rule for them with matplotlib.ticker, make them say rupees, per cent or thousands, and label dates by day or month with matplotlib.dates. 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
Lalitha drew the investors' chart of SaafHawa's monthly recurring revenue (MRR) from business_monthly.csv: 30 months, from ₹9,700 in April 2024 to ₹39.05 lakh in September 2026. The line is right. Nobody can read it.
<!-- chart: !MRR rising from near 0 to 4.0 on a y axis marked 1e6 in its corner, over 30 month labels run together into a black smudge -->
Up the y axis it says 0.0 to 4.0, with a small 1e6 in the corner: one investor read it as ₹4. Along the x axis, all 30 months have a label, and they run into one black smudge. Jaspreet has the same problem with his hourly charts: the x axis says 0, 5, 10, 15, 20, and a principal thinks in 6 am and noon.
Everything you have drawn so far had its ticks chosen for you. The numbers are right, but they are Matplotlib's numbers, not the reader's: plain values where the reader wants ₹ lakh, every fifth hour where she wants clock times, every month where there is room for six. This chapter takes the ticks over.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
clock_ticks.py: clock times every 3 hours
Write clock_chart(hours, pm25) in clock_ticks.py. hours is a list of whole hours of one day, in order (0 to 23 for a whole day, 6 to 18 for part of one), and pm25 the PM2.5 reading at each hour. It makes a new Figure and Axes, draws pm25 against hours as one line, and returns the Figure (fig itself, with exactly one Axes and one line).
The x axis has a tick at every hour of the data that divides by 3, and no other tick: for 0 to 23 that is 0, 3, 6 up to 21; for 7 to 14 it is 9 and 12. Each is labelled with the hour in two digits, a colon and 00: 00:00, 03:00, 12:00 (f"{h:02d}" writes 3 as 03).
The y axis has a tick at every multiple of 60 that the chart shows (60 µg/m³ is India's 24-hour standard), and none in between.
The Check calls it with Delhi's 24 hours of 5 January 2026 (delhi_hourly.csv), Anand Vihar from 6 am to 6 pm on 22 October 2025 and Saket from 7 am to 2 pm on 16 October 2025 (diwali_hourly.csv), and reads the ticks a reader sees after the figure is drawn.
- Task 2
month_ticks.py: a year labelled by month
Write month_chart(dates, pm25) in month_ticks.py. dates is a pandas Series of dates (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 x axis has a tick on the 1st of every month that the chart shows, and no other tick, each labelled with the month's three-letter short name: Oct, Nov, Dec, Jan. (The chart shows a little space past each end of the data, so a year from 1 October can show the next 1 October too: label it Oct as well.)
The Check calls it with Delhi's whole year (1 October 2025 to 30 September 2026) and with Noida from 1 December 2025 to 28 February 2026, and reads the ticks a reader sees after the figure is drawn.
- Task 3
lakh_ticks.py: MRR in rupees lakh
Write mrr_chart(months, mrr_rs) in lakh_ticks.py. months is a pandas Series of dates (the month column of business_monthly.csv after pd.to_datetime) and mrr_rs the monthly recurring revenue in rupees (₹9,700 to ₹39,05,400). It makes a new Figure and Axes, draws mrr_rs against months as one line, with the rupees as they are (not divided), and returns the Figure (one Axes, one line).
Leave where the y ticks go to Matplotlib, and change only what they say: each y tick label is its value in lakh (1 lakh is ₹1,00,000) with one decimal, the ₹ sign in front and a capital L after. 2,50,000 is ₹2.5L, 35,00,000 is ₹35.0L, 0 is ₹0.0L. The starter keeps the ₹ sign in RUPEE.
The Check calls it with all 30 months and with the first 10 (April 2024 to January 2025), whose ticks fall at other values, and compares every y label a reader sees with its tick's value.
- Task 4 · interview question
Interview: new_customers.py, the black smudge
Lalitha's customers_chart(months, new_customers) draws the new customers of each month as a line titled New customers each month, and returns the Figure. months is a list of text months as business_monthly.csv has them ("2024-04", "2024-05"), new_customers a count for each. The line is right, but every month has its own label under the x axis, and on a 640-pixel chart they run together into one black smudge.
Fix it so the x axis can be read. It still returns a Figure with one Axes and one line through the months in order, new_customers up y, titled New customers each month (exactly). The x axis must show at least two labels, no two of them overlapping and none running off the edge of the figure. Any fix that does this passes.
The Check calls it with all 30 months (April 2024 to September 2026) and with April 2025 to March 2026, draws the figure, and measures the box of every x label a reader sees.
Tanvir will ask: why did every month get a label of its own, and what decides where an axis puts its ticks and what they say?
What you will learn
- A tick: a place on an axis (a locator decides where) and its words (a formatter decides what).
- ax.set_xticks(places, labels) and ax.set_yticks: ticks by hand.
- ax.tick_params: turn the labels, change their size.
- matplotlib.ticker: MultipleLocator, MaxNLocator, StrMethodFormatter, FuncFormatter, PercentFormatter.
- matplotlib.dates: DayLocator, MonthLocator, DateFormatter, ConciseDateFormatter.
- Fewer ticks read better, and ax.get_xticklabels() to read them back.
More in Reading the numbers
Build "Ticks and formatters" 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.