Menu
CoddyTech

Scatter Plots

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 21 of 60.

Ten houseplants: hours of sun a day, and how much each grew. Joined by a line, they make a tangle. As dots, a pattern appears:

Two charts of the same ten plants, sun across and growth up. Left, plt.plot joins them into a zigzag tangle. Right, plt.scatter shows dots rising to the right

plt.scatter(x, y) draws one dot per pair, with no lines:

sun = [2, 6, 3, 8, 5, 1, 7, 4, 6, 3]
growth = [4, 10, 7, 12, 6, 2, 9, 8, 7, 3]
plt.scatter(sun, growth)
plt.xlabel("Sun (hours a day)")
plt.ylabel("Growth (cm)")
plt.show()

More sun, more growth. A scatter plot shows how two measurements of separate samples relate: the first look before a model predicts one from the other.

Values in order, like the days of a week, still get plt.plot.

challenge icon

Challenge

Medium

An angler logged each fish in catch order: one row per fish, length (cm) then weight (g). The starter joins them with plt.plot, so the line zigzags and hides the pattern.

  1. Draw every fish as its own dot: length across, weight up.
  2. Leave no line at all.
  3. Keep the title, the labels and plt.show().

Yours should look like this one, from another trip:

A scatter plot titled Fish Catch: nine dots rising from 18 cm and 90 g to 42 cm and 980 g, with no line

Your program prints nothing. Coddy describes your chart in the output.

Try it yourself

import numpy as np
import matplotlib.pyplot as plt

# Each row is one fish, in the order it was caught: length (cm), weight (g)
fish = np.array([[22, 180], [30, 390], [25, 250], [35, 620],
                 [28, 330], [40, 870], [19, 120], [33, 510]])

# Fix the drawing below
plt.plot(fish[:, 0], fish[:, 1])

# Don't change the lines below
plt.title("Fish Catch")
plt.xlabel("Length (cm)")
plt.ylabel("Weight (g)")
plt.show()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler