Exercises
These exercises ask you to use plots as inspection tools.
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.
Edit the code so it creates a labeled line plot and prints points: 4.
Labeled line plot
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.
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.
Edit the code so it displays A as a color grid and prints its shape.
Inspect a matrix
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.
A plot shows that the computed curve is straight, but the expected curve bends upward. What is the most useful next action?
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.