Sea level predictor: test_plot_lines

FAIL: test_plot_lines (test_module.LinePlotTestCase)

Traceback (most recent call last):
File “/home/runner/boilerplate-sea-level-predictor-1/test_module.py”, line 34, in test_plot_lines
self.assertEqual(actual, expected, “Expected different line for first line of best fit.”)
AssertionError: Lists differ: [0.0, 0.220472441, -0.440944881, -0.232283[1677 chars]4951] != [-0.5421240249263661, -0.4790794409142336,[3256 chars]2443]

First differing element 0:
0.0
-0.5421240249263661

Second list contains 36 additional elements.
First extra element 134:
7.905850232699706

Diff is 5939 characters long. Set self.maxDiff to None to see it. : Expected different line for first line of best fit.

what is the reason behind it?

The error message states that your first line of best fit is wrong.
Your numbers indicate it’s not even a straight line.

So you gotta work on that.
Also pretty sure you are supposed to save the plot, looking at that might help understand the error.

Finally, please provide a link to your code if you want further help.
This way people can actually tell you where the error comes from.

thnks for the response
here’s the code:
‘’’
def draw_plot():
# Read data from file
df = pd.read_csv(“epa-sea-level.csv”)
#float_precision=‘legacy’
# Create scatter plot
plt.scatter(df.Year, df[“CSIRO Adjusted Sea Level”])
plt.xlabel(‘Year’)
plt.ylabel(‘CSIRO Adjusted Sea Level’)

# Create first line of best fit
year1 = list(range(1880, 2050)) 
year1 = pd.Series(year1)
year2 = list(range(2000, 2050)) 
year2 = pd.Series(year2)

res = linregress(df.Year, df["CSIRO Adjusted Sea Level"])
df2 = df[df.Year>= 2000]
res2 = linregress(df2.Year, df2["CSIRO Adjusted Sea Level"])

plt.figure(figsize=(10,5))
plt.plot(df.Year, df["CSIRO Adjusted Sea Level"], 'o', markersize = 5.2, label='original data')
plt.plot(year1, res.intercept + res.slope*year1, 'r', linewidth=2.5, label='fitted line 1')

# Create second line of best fit
plt.plot(year2, res2.intercept + res2.slope*year2, 'r', linewidth=2.5, label='fitted line 2', color='green')
plt.legend()

# 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()

‘’’

Hi. Could you solve the problem?. I got to this part and I am getting the exact same message, but I think I am doing the right procedure…

Thank you.

In case you are still stuck on this…
From looking at your code, it seems that you plot the actual data twice, once as an actual scatter plot (plt.scatter) and one time as a line plot (plt.plot) - this one is not needed. The test expects one scatter plot and two line plots (from the fits), and any additional lines can result in a test failure.
The next thing you’ll most likely run into is some test failure connected to “shape”. Look at your year1 and year2 ranges and check whether they actually include the year 2050…!
Good luck :slight_smile:

1 Like

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