Matplotlib · Chapter 7 of 16

Distributions: histograms and box plots

By the end of this chapter you can show how a year of readings is spread out with ax.hist, choose its bins (as a number or as edges), put two histograms on one Axes, mark the mean and the median and say why they differ, compare many groups with ax.boxplot, and read a box, its whiskers and its fliers. 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

Ira's notes to the schools have quoted the year's mean, and a principal wrote back: "Delhi averaged 105.3. Is that a normal day for us?" It is not. On 226 of Delhi's 365 days the air was better than the mean; a few terrible winter days pulled it up. Lalitha needs the societies' report to say how many days were Good, Moderate or Severe, and Jaspreet wants to know which month's readings were odd.

These questions are not about when. Shuffle Delhi's 365 days into any order and the answers stay the same: how many days were under 60, where the middle is, how far the bad days reach. That is a distribution: what values occur, and how often. A line chart of the year shows the order, which you do not need, and hides the counts, which you do. A histogram counts.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    aqi_hist.py: the days in each AQI band

    Write aqi_hist(pm25, place) in aqi_hist.py. pm25 is a pandas Series of one place's daily PM2.5 from city_daily.csv. It makes a new Figure and Axes and draws one histogram of pm25 whose bins are the six AQI bands: the bin edges exactly AQI_EDGES from the starter, [0, 30, 60, 90, 120, 250, 500] (counts, or density=True, either is right). It gives the chart exactly:

    title: Days in each AQI band, <place> (aqi_hist(pm25, "Noida") is titled Days in each AQI band, Noida)

    x axis label: PM2.5 (µg/m³), which the starter keeps in Y_LABEL

    y axis label: Days

    It returns the Figure, with one Axes holding that one histogram. The Check counts the days in each band itself and compares every bar, and tries Noida's year, Faridabad's year and Gurugram's July (placed as "Gurugram in July").

  2. Task 2

    two_hists.py: two histograms on one Axes

    Write compare_hist(first, second, names) in two_hists.py. first and second are pandas Series of daily PM2.5 from city_daily.csv, and names is a list of two names for them, in the same order. It makes a new Figure and Axes and draws two histograms on that one Axes, first's and then second's, as bars (the default kind, so give them some transparency, such as alpha=0.5, to see both). Both must use exactly the same bin edges, and the edges must cover every value of both (the starter's EDGES, 0 to 400 in steps of 20, does). It adds a legend whose two entries are exactly the two names.

    It returns the Figure. The Check counts each set of days with your edges, checks that both histograms have the same edges, and reads the legend. It tries Gurugram against Ghaziabad over the year, and Noida against Faridabad from November to January (names "Noida, winter" and "Faridabad, winter").

  3. Task 3

    city_boxes.py: one box per city

    Write city_boxes(city_days, order) in city_boxes.py. city_days is the whole of city_daily.csv as a DataFrame (it has a city column and a pm25 column), and order is a list of city names. It makes a new Figure and Axes and draws one upright box plot (ax.boxplot) with one box per city in order, left to right, each box made from that city's daily pm25, and each box labelled under it with its city's name (tick_labels=). It labels the y axis PM2.5 (µg/m³) (Y_LABEL) and returns the Figure.

    The Check reads each box's median, its 25th and 75th percentiles and its label, and tries the five cities in the order Gurugram, Noida, Delhi, Faridabad, Ghaziabad, then three of them as Ghaziabad, Delhi, Gurugram.

  4. Task 4 · interview question

    Interview: mean_median.py, the mean and the median

    Write mean_median(pm25, place) in mean_median.py. pm25 is a pandas Series of daily PM2.5 with no blanks (a city's year from city_daily.csv, or one station's month from daily.csv). It makes a new Figure and Axes and draws a histogram of pm25 (any bins), a vertical line (ax.axvline) at its mean labelled Mean and the mean with one decimal, a vertical line at its median labelled Median and the median with one decimal (such as Mean 123.4 and Median 98.7, made with f-strings from the data), a legend with exactly those two entries, and the title PM2.5 in <place>, day by day. It returns the Figure.

    The Check computes the mean and the median itself and tries Ghaziabad's year, Gurugram's year and Hauz Khas's March 2026 (placed as "Hauz Khas in March"), the month of the spider's 1,485.3.

    Tanvir will ask: why is the mean so far above the median, and which one would you tell a principal is a typical day?

What you will learn

  • ax.hist(values, bins=): count how many values fall into each bin and draw a bar for each.
  • Bins: as a number or as a list of edges, and how they change the story.
  • density=True, alpha=, histtype="step": two histograms on one Axes.
  • The shape: a right skew, the mean against the median.
  • ax.boxplot: the middle half, the median, the whiskers and the fliers, one box per group, with tick_labels= and orientation=.
  • ax.violinplot: the whole shape, on both sides.

More in Reading the numbers

Build "Distributions: histograms and box plots" 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.