forked from freeCodeCamp/boilerplate-sea-level-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsea_level_predictor.py
32 lines (26 loc) · 1.32 KB
/
sea_level_predictor.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
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")
# Create scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(df['Year'], df['CSIRO Adjusted Sea Level'], label='Data', color='b')
# Create first line of best fit using all available data
slope1, intercept1, r_value1, p_value1, std_err1 = linregress(df['Year'], df['CSIRO Adjusted Sea Level'])
years_extended = pd.Series([i for i in range(1880, 2051)])
plt.plot(years_extended, intercept1 + slope1 * years_extended, label='Best Fit Line 1880-2050', color='r')
# Create second line of best fit using data from 2000 onwards
df_recent = df[df['Year'] >= 2000]
slope2, intercept2, r_value2, p_value2, std_err2 = linregress(df_recent['Year'], df_recent['CSIRO Adjusted Sea Level'])
years_recent_extended = pd.Series([i for i in range(2000, 2051)])
plt.plot(years_recent_extended, intercept2 + slope2 * years_recent_extended, label='Best Fit Line 2000-2050', color='g')
# Add labels and title
plt.xlabel('Year')
plt.ylabel('Sea Level (inches)')
plt.title('Rise in Sea Level')
plt.legend()
# Save plot and return data for testing (DO NOT MODIFY)
plt.savefig('sea_level_plot.png')
return plt.gca()