Exercises

These exercises ask you to use plots as inspection tools.

Exercise: Line plot lengths

If x.shape is (5,), what shape should y have for a simple line plot?

Answer it first, then check.

Hint

A simple line plot pairs each x with one y.

Solution

The shape should be:

(5,)

A simple line plot pairs each x value with one y value.

Exercise: Make a labeled curve

Edit the code so it creates a labeled line plot and prints points: 4.

Labeled line plot

Runs locally with Python in your browser.

Ready to run.

Hint

Use plt.plot(x, y, marker="o"), then add xlabel, ylabel, and title. Print len(x).

Solution

One fix is:

The labels make the plotted quantities clear.

Exercise: Choose a histogram

Which Matplotlib function makes a histogram?

Answer it first, then check.

Hint

The function name is the first four letters of histogram.

Solution

Use:

plt.hist(values)

hist makes a histogram.

Exercise: Plot a matrix

Edit the code so it displays A as a color grid and prints its shape.

Inspect a matrix

Runs locally with Python in your browser.

Ready to run.

Hint

Use:

plt.imshow(A)

Then print A.shape.

Solution

One fix is:

imshow displays the two-dimensional array as a color grid.

Exercise: Write the observation

A plot shows that the computed curve is straight, but the expected curve bends upward. What is the most useful next action?

Choose one

Select one choice, then check.

Hint

If the curve has the wrong shape, inspect the expression that computed it.

Solution

Check the formula that produced the computed curve.

The visual mismatch is evidence. A straight computed curve where a curved result is expected often means the expression is doing the wrong operation.