-
Notifications
You must be signed in to change notification settings - Fork 0
/
sea-level.py
38 lines (32 loc) · 1.18 KB
/
sea-level.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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")
y = df["CSIRO Adjusted Sea Level"]
x = df["Year"]
# Create scatter plot
fig, ax = plt.subplots()
plt.scatter(x, y)
# Create first line of best fit
res = linregress(x, y)
print(res)
x_pred = pd.Series([i for i in range(1880, 2051)])
y_pred = res.slope*x_pred + res.intercept
plt.plot(x_pred, y_pred + "r")
# Create second line of best fit
new_df = df.loc[df['Year'] >= 2000]
new_x = new_df['Year']
new_y = new_df["CSIDRO Adjusted Sea Level"]
res_2 = linregress(new_x, new_y)
x_pred2 = pd.Series([i for i in range(2000,2051)])
y_pred2 = res_2.slope*x_pred2 + res_2.intercept
plt.plot(x_pred2, y_pred2, 'green')
# Add labels and title
ax.set_xlabel('Year') #Add an x-label to the axes
ax.set_ylabel('Sea Level (inches)') #Add a y-label to the axes.
ax.set_title('Rise in Sea Level')
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()