@sanity @yorgyetson @mtrworld
Trying to get the “First Line of Best Fit” where we are supposed to predict sea level rise in 2050 by making the line go through the year 2050. I am able to get the line go through scatter plot yet I can’t see how can I make 2050 appear in the graph to let the best fit line pass through it?
def draw_plot():
# Read data from file
df = pd.read_csv('epa-sea-level.csv')
# Create scatter plot
# plt.figure(figsize=(12,8))
# ax = plt.subplot(1,2,1)
# ax.scatter(x='Year', y ='CSIRO Adjusted Sea Level')
x = df['Year']
y = df['CSIRO Adjusted Sea Level']
plt.xlabel('Year')
plt.ylabel('CSIRO Adjusted Sea Level')
plt.scatter(x,y)
# Create first line of best fit
x = df['Year']
y = df['CSIRO Adjusted Sea Level']
slope, intercept, r_value, p_value, std_err = linregress(x,y)
plt.plot(x, intercept + slope*x, 'r', label = 'Fitted Line 1')
plt.legend()
plt.show()
Once you have the slope and the intercept for (x, y)
data, you can create a dummy variable
x2 =list(range(start, end))
with start = 1880
and end = 2500
and use that in your plot .
Edit: The list will be from 1880 to 2049. However, when you run your code on repl.it, the test_module.py excludes 2050, so it works out.
@kitanikita
Followed your suggestion; seems close but…if you can guide me what I’m doing wrong in here:
# Dummy variable to calculate First best fit line
x2 = list(range(1880, 2050))
x2 = list(map(int, x2))
plt.plot(x2, intercept + slope*x2, 'r', label = 'Bests Fit Line 1')
plt.legend()
plt.show()
File “main.py”, line 6, in
sea_level_predictor.draw_plot()
File “/home/runner/MistyGrowingBrace/sea_level_predictor.py”, line 34, in d
raw_plot
plt.plot(x2, intercept + slope*x2, ‘r’, label = ‘Bests Fit Line 1’)
TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’
Sorry about that, I forgot that you can’t do linear algebra on lists in python. You will have to create an empty dummy list y2
and loop through the x2
list to fill it with the append()
function.
x2 = list(range(0, 10))
y2 = []
for year in x2:
y2.append(intercept + slope*year)
Then you can plot x2
and y2
, as normal.
plt.plot(x2, y2, 'r', label = 'Bests Fit Line 1')
Alternatively, you can use numpy
arrays, which allow you to perform linear algebra stuff with them. However, numpy
was not part of the import section of the starter code for this task. So I assume they expect us to do something like this without using numpy
.
2 Likes