Sea Level Predictor:Expected different line for first line of best fit

Hi. I have this code and it seems to work fine, but I always get this errors.

Your code so far
def draw_plot():
# Read data from file
df=pd.read_csv(‘epa-sea-level.csv’)

# Create scatter plot using the "Year" column as the x-axis and the "CSIRO Adjusted Sea Level" column as the y-axix.
fig, ax = plt.subplots(figsize=(10,5))
plt.scatter(df.Year, df["CSIRO Adjusted Sea Level"])


# Create first line of best fit

# Use the linregress function from scipy.stats to get the slope and y-intercept of 
# the line of best fit. Plot the line of best fit over the top of the scatter plot.
# Make the line go through the year 2050 to predict the sea level rise in 2050.
x = df['Year']
y = df['CSIRO Adjusted Sea Level']

slope, intercept, r_value, p_value, std_err = linregress(x,y)

Dummy variable to calculate First best fit line

x1 = list(range(1880, 2050))
y1 = []

for year in x1:
    y1.append(intercept + slope * year)
    
ax1 = plt.plot(x1, y1, 'r', label = 'Best Fit Line 1', color='red')
plt.legend()

# Create second line of best fit
xx = df[ df['Year'] >= 2000 ]['Year']
yy = df[ df['Year'] >= 2000 ]['CSIRO Adjusted Sea Level']
 
fit2 = linregress(xx, yy)
new_slope = fit2.slope
new_intercept = fit2.intercept

x2 = list(range(2000, 2050))
y2 = []

for year in x2:
    y2.append(new_intercept + new_slope * year)

ax2 = plt.plot(x2, y2, 'r', label = 'Best Fit Line 2', color='green')
plt.legend()


# Add labels and title
ax.set_xlabel('Year')
ax.set_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()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Sea Level Predictor

Link to the challenge:

Take a look at the test output. Notice there’s mentioned mismatch in shapes for the first line.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.