When I run my code in replit, I get the following error:
AssertionError:
Arrays are not almost equal to 7 decimals
Expected different line for first line of best fit.
(shapes (170,), (171,) mismatch)
x: array([-0.542124 , -0.4790794, -0.4160349, -0.3529903, -0.2899457,
-0.2269011, -0.1638565, -0.1008119, -0.0377674, 0.0252772,
0.0883218, 0.1513664, 0.214411 , 0.2774556, 0.3405002,...
y: array([-0.542124 , -0.4790794, -0.4160349, -0.3529903, -0.2899457,
-0.2269011, -0.1638565, -0.1008119, -0.0377674, 0.0252772,
0.0883218, 0.1513664, 0.214411 , 0.2774556, 0.3405002,...
I understand that issues similar to this have been opened before. I have reviewed them so that I could troubleshoot the issue myself.
I ran my code in a jupyter notebook cell and saw that the graph matches the figure provided in the repl.
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
def draw_plot():
# Read data from file
df = pd.read_csv('epa-sea-level.csv')
# Create scatter plot
plt.scatter(df['Year'], df['CSIRO Adjusted Sea Level'])
# Create first line of best fit
res = linregress(df['Year'], df['CSIRO Adjusted Sea Level'])
x = range(1880, 2050)
y = res.intercept + res.slope * x
plt.plot(x, y, 'r')
# Create second line of best fit
x_2 = df[df['Year'] >= 2000]['Year']
res_2 = linregress(x_2, df[df['Year'] >= 2000]['CSIRO Adjusted Sea Level'])
y_2 = res_2.intercept + res_2.slope * x_2
plt.plot(x_2, y_2, 'y')
# Add labels and title
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title('Rise in Sea Level')
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()
The line of best fit needs to go to 2050 (both of them)
Keep in mind range() is not inclusive of the last number. range(0,5) returns 0,1,2,3,4 and never makes it to the number 5. It returns 5 numbers: 0 - 4.