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:
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
MediumAn 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.
- Draw every fish as its own dot: length across, weight up.
- Leave no line at all.
- Keep the title, the labels and
plt.show().
Yours should look like this one, from another trip:
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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature Grid5Line and Scatter Plots
Labels and TitlesScatter PlotsSeveral Lines and LegendsColors and MarkersFigures and AxesRecap: Weekly StepsPractice on your own: Online Python compiler