Hello, I’m having some trouble trying to fix this issue with my plot. Apparently there is a mismatch in shape and in the expected lines from the arrays. However, when looking at my graph I feel like it looks exactly as it is supposed to look like, as they ask for two lines predicting the sea level behavior for 2050.
This is my graph:
This is my code:
# Read data from file
df = pd.read_csv('epa-sea-level.csv')
# Create scatter plot
y = df['CSIRO Adjusted Sea Level']
x = df['Year']
plt.scatter(x, y)
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")
# Create first line of best fit
slp = linregress(x, y)
new = {'Year': 2050}
df = df.append(new, ignore_index=True)
y = df['CSIRO Adjusted Sea Level']
x = df['Year']
plt.plot(x, y, 'o')
plt.plot(x, slp[0] * x + slp[1])
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")
# Create second line of best fit
dfb = df[(df['Year'] >= 2000) & (df['Year'] < 2050)]
yb = dfb['CSIRO Adjusted Sea Level']
xb = dfb['Year']
slpb = linregress(xb, yb)
new = {'Year': 2050}
dfb = dfb.append(new, ignore_index=True)
yb = dfb['CSIRO Adjusted Sea Level']
xb = dfb['Year']
plt.plot(x, y, 'o')
plt.plot(x, slp[0] * x + slp[1])
plt.plot(xb, slpb[0] * xb + slpb[1])
# Add labels and title
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title("Rise in Sea Level")
This is the error I’m getting:
AssertionError:
Arrays are not almost equal to 7 decimals
Expected different line for first line of best fit.
(shapes (135,), (171,) mismatch)
x: array([ 0. , 0.2204724, -0.4409449, -0.2322835, 0.5905512,
0.5314961, 0.4370079, 0.2165354, 0.2992126, 0.3622047,
0.4409449, 0.3740157, 0.5 , 0.6850394, 0.3031496,…
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,…
Ran 4 tests in 0.279s
FAILED (failures=1)
Challenge: Sea Level Predictor
Link to the challenge: