0
0
Matplotlibdata~10 mins

Trend lines on scatter plots in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to plot a scatter plot using matplotlib.

Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.[1](x, y)
plt.show()
Drag options to blanks, or click blank then click option'
Ascatter
Bplot
Cbar
Dhist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot instead of plt.scatter for scatter plots.
Using plt.bar which is for bar charts.
2fill in blank
medium

Complete the code to calculate the slope and intercept of a linear trend line using numpy.

Matplotlib
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 5, 7, 11])

slope, intercept = np.polyfit(x, y, [1])
Drag options to blanks, or click blank then click option'
A0
B2
C3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or higher degree which fits curves, not lines.
Using 0 which is a constant.
3fill in blank
hard

Fix the error in the code to plot the trend line on the scatter plot.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 5, 7, 11])

slope, intercept = np.polyfit(x, y, 1)

plt.scatter(x, y)
plt.plot(x, slope * [1] + intercept, color='red')
plt.show()
Drag options to blanks, or click blank then click option'
Ay
Bx
Cslope
Dintercept
Attempts:
3 left
💡 Hint
Common Mistakes
Using y instead of x in the line equation.
Using slope or intercept alone without x.
4fill in blank
hard

Fill both blanks to create a dictionary of squared values for numbers greater than 3.

Matplotlib
numbers = [1, 2, 3, 4, 5]
squares = {num: num[1]2 for num in numbers if num [2] 3}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of ** for power.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and values greater than 0.

Matplotlib
data = {'a': 1, 'b': -2, 'c': 3}
result = [1]: [2] for [3], [2] in data.items() if [2] > 0
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' as loop variable without unpacking.
Not converting keys to uppercase.