Displaying the linear regression results
Now, you will display your linear regression results on the scatter plot, the code for which is already pre-written for you from your previous exercise. To do this, take the first 100 bootstrap samples (stored in bs_slope_reps_1975
, bs_intercept_reps_1975
, bs_slope_reps_2012
, and bs_intercept_reps_2012
) and plot the lines with alpha=0.2
and linewidth=0.5
keyword arguments to plt.plot()
.
This exercise is part of the course
Statistical Thinking in Python (Part 2)
Exercise instructions
- Generate the \(x\)-values for the bootstrap lines using
np.array()
. They should consist of 10 mm and 17 mm. - Write a
for
loop to plot 100 of the bootstrap lines for the 1975 and 2012 data sets. The lines for the 1975 data set should be'blue'
and those for the 2012 data set should be'red'
. - Hit submit to view the plot!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Make scatter plot of 1975 data
_ = plt.plot(bl_1975, bd_1975, marker='.',
linestyle='none', color='blue', alpha=0.5)
# Make scatter plot of 2012 data
_ = plt.plot(bl_2012, bd_2012, marker='.',
linestyle='none', color='red', alpha=0.5)
# Label axes and make legend
_ = plt.xlabel('beak length (mm)')
_ = plt.ylabel('beak depth (mm)')
_ = plt.legend(('1975', '2012'), loc='upper left')
# Generate x-values for bootstrap lines: x
x = np.array([____, ____])
# Plot the bootstrap lines
for i in range(100):
plt.plot(____, ____,
linewidth=0.5, alpha=0.2, color=____)
plt.plot(____, ____,
linewidth=0.5, alpha=0.2, color=____)
# Draw the plot again
plt.show()