Matplotlib · Chapter 5 of 16

Scatter plots and relationships

By the end of this chapter you can draw one point per row with ax.scatter, read a relationship between two columns from it, show a third column as colour with a colorbar (and others as size and shape), keep 16,000 points readable, and end the chart with a sentence: the correlation and a fitted line. 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

Every winter Jaspreet gets the same call: "the sensor is broken, it has read over 200 for three days". It is not broken. Those were still, cold days, and the smoke had nowhere to go. Ira wants to put that in the schools' notes: windy days are cleaner, so a calm forecast means plan the assembly indoors. Before she says it to 15 principals, she wants to see it.

Everything you have drawn so far is a line: values in order, one after another, along hours or days. Wind against PM2.5 is not a sequence. Each day is a pair (that day's wind, that day's PM2.5), and the question is whether the pairs lean one way: do high winds go with low readings? Two lines over the year, one for wind and one for PM2.5, make the reader compare dates by eye. And ax.plot(wind, pm25) draws something worse, as you will see: a scribble.

What you build: 4 tasks

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

  1. Task 1

    wind_scatter.py: one point per day

    Write wind_scatter(wind, pm25, city) in wind_scatter.py. wind and pm25 are one city's days from city_daily.csv, its wind_kmh and pm25 columns, day by day (pandas Series or lists). It makes a new Figure and Axes, draws one point per day with the Axes' scatter method, wind along x and PM2.5 up y, and no line, and gives the chart exactly:

    title: PM2.5 and wind in <city>, the words PM2.5 and wind in, a space, then the city it is given (wind_scatter(wind, pm25, "Gurugram") is titled PM2.5 and wind in Gurugram)

    x axis label: Wind (km/h)

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

    It returns the Figure, with exactly one Axes holding one scatter of every day's (wind, PM2.5) pair. The Check calls it with Gurugram's whole year (Series), Noida's November to January (lists) and three days of Delhi's July (lists).

  2. Task 2

    temp_colour.py: a third column as colour

    Write temp_scatter(wind, pm25, temp) in temp_colour.py. wind, pm25 and temp are pandas Series of one city's days from city_daily.csv (wind_kmh, pm25 and temp_c), in the same day order. It makes a new Figure and Axes and draws one scatter, wind along x and PM2.5 up y, with each point coloured by its day's temperature: give scatter the temperatures with c= (the default colormap is right). It adds a colorbar for those points with fig.colorbar, labelled exactly Temperature (°C) (the starter keeps it in C_LABEL), labels the x axis Wind (km/h) and the y axis PM2.5 (µg/m³) (Y_LABEL), and returns the Figure.

    The Figure has one chart and one colorbar (the colorbar is an Axes of its own; the Check does not count it as a chart). The chart holds one scatter and no line, and the colour values of its points are the temperatures, each on its own day's point. The Check tries Faridabad's whole year and Ghaziabad's March to May.

  3. Task 3

    wind_fit.py: the fitted line, the slope and the correlation

    Write wind_fit(wind, pm25) in wind_fit.py. wind and pm25 are pandas Series of one city's days from city_daily.csv (wind_kmh and pm25). It fits a straight line to PM2.5 (y) against wind (x) with np.polyfit(wind, pm25, 1), finds the correlation of wind and PM2.5 with np.corrcoef (the one number off the diagonal of the 2 by 2 it returns), and makes a new Figure and Axes with two things on it: one scatter of the days (wind along x) and one line, the fitted line, drawn from the smallest wind in the data to the largest (every point of the line must lie on the fitted straight line).

    It returns three things, in this order: fig, slope, r (the Figure, the fitted slope in µg/m³ per km/h, and the correlation, both as numbers). The Check compares the slope and the correlation with its own, and tries Ghaziabad's year, Faridabad's year and six days of Gurugram's December.

  4. Task 4 · interview question

    Interview: sorted_line.py, a saw instead of a cloud

    Jaspreet's wind_days(wind, pm25) drew wind against PM2.5 with plot and got a scribble; he sorted the days by wind (keeping each day's pair together) and plotted again, and now the chart is a jagged saw running left to right, up and down at every step. wind and pm25 are lists of one city's days from city_daily.csv (wind_kmh and pm25). Fix it so the Figure it returns has one Axes holding one scatter of the days, every (wind, PM2.5) pair as a point, wind along x, and no line. Keep the title Wind and PM2.5, day by day, the x axis label Wind (km/h) and the y axis label PM2.5 (µg/m³) (Y_LABEL) as they are. The Check tries Gurugram's November to January and Noida's April to June.

    Tanvir will ask: scatter or plot for wind against PM2.5, why did plot draw a scribble, and why did sorting by wind not fix it?

What you will learn

  • ax.scatter(x, y): one point per pair, no line, no order.
  • A relationship: reading the cloud's lean and spread.
  • c= and fig.colorbar: a third column as colour, and the key that reads it.
  • s=, marker=, edgecolors=: size, shape and outline of each point.
  • Overplotting: alpha=, small s=, and ax.hexbin for thousands of points.
  • np.corrcoef and np.polyfit: the sentence at the end, one number and a line.

More in The first chart

Build "Scatter plots and relationships" 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.