-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsea_level_predictor.py
More file actions
42 lines (35 loc) · 1.45 KB
/
sea_level_predictor.py
File metadata and controls
42 lines (35 loc) · 1.45 KB
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
39
40
41
42
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
def draw_plot():
# Read data
df = pd.read_csv("epa-sea-level.csv")
# Create scatter plot
fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(df['Year'], df['CSIRO Adjusted Sea Level'], color='blue', label='Data')
# -----------------------
# Line of best fit (all years)
# -----------------------
slope_all, intercept_all, r_value, p_value, std_err = linregress(df['Year'], df['CSIRO Adjusted Sea Level'])
# Extend the line to 2050
years_all = pd.Series(range(int(df['Year'].min()), 2051))
line_all = intercept_all + slope_all * years_all
ax.plot(years_all, line_all, color='red', label='Best Fit (All Years)')
# -----------------------
# Line of best fit (from 2000)
# -----------------------
df_recent = df[df['Year'] >= 2000]
slope_recent, intercept_recent, r_value, p_value, std_err = linregress(df_recent['Year'], df_recent['CSIRO Adjusted Sea Level'])
years_recent = pd.Series(range(2000, 2051))
line_recent = intercept_recent + slope_recent * years_recent
ax.plot(years_recent, line_recent, color='green', label='Best Fit (2000+)')
# -----------------------
# Labels and title
# -----------------------
ax.set_xlabel("Year")
ax.set_ylabel("Sea Level (inches)")
ax.set_title("Rise in Sea Level")
ax.legend()
# Save figure
fig.savefig('sea_level_plot.png')
return fig